-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (82 loc) · 2.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express');
const mysql = require('mysql2');
const path = require('path');
const cors = require('cors');
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'sudo',
database: 'nodemysl'
});
db.connect(err => {
if (err) {
console.error('Error connecting to database:', err);
process.exit(1);
}
console.log('mySQL connected');
});
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.use(express.json());
app.get('/createdb', (req, res) => {
let sql = 'CREATE DATABASE IF NOT EXISTS nodemysl';
db.query(sql, err => {
if (err) {
throw err;
}
res.send('Database created or already exists');
});
});
app.get('/createemployee', (req, res) => {
let sql = 'CREATE TABLE IF NOT EXISTS employee(id int AUTO_INCREMENT, name VARCHAR(255), designation VARCHAR(255), phone_number VARCHAR(255), email VARCHAR(255), PRIMARY KEY(id))';
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.send('Employee table created or already exists');
});
});
app.post('/addemployee', (req, res) => {
const { name, designation, phone_number, email } = req.body;
const sql = 'INSERT INTO employee (name, designation, phone_number, email) VALUES (?, ?, ?, ?)';
db.query(sql, [name, designation, phone_number, email], (err, result) => {
if (err) {
throw err;
}
res.send('Employee added successfully');
});
});
app.put('/updateemployee/:id', (req, res) => {
const { id } = req.params;
const { name, designation, phone_number, email } = req.body;
const sql = 'UPDATE employee SET name = ?, designation = ?, phone_number = ?, email = ? WHERE id = ?';
db.query(sql, [name, designation, phone_number, email, id], (err, result) => {
if (err) {
throw err;
}
res.send('Employee updated successfully');
});
});
app.delete('/deleteemployee/:id', (req, res) => {
const { id } = req.params;
const sql = 'DELETE FROM employee WHERE id = ?';
db.query(sql, [id], (err, result) => {
if (err) {
throw err;
}
res.send('Employee deleted successfully');
});
});
app.get('/getemployee', (req, res) => {
let sql = 'SELECT * FROM employee';
db.query(sql, (err, result) => {
if (err) {
throw err;
}
res.send(result);
});
});
app.listen('5500', () => {
console.log('Server started at port 5500');
});