-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.js
63 lines (54 loc) · 2 KB
/
server.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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json());
const db = new sqlite3.Database(':memory:');
db.serialize(() => {
db.run("CREATE TABLE employees (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, position TEXT, salary REAL)");
const stmt = db.prepare("INSERT INTO employees (name, position, salary) VALUES (?, ?, ?)");
stmt.run("John Doe", "Developer", 60000);
stmt.run("Jane Smith", "Manager", 80000);
stmt.run("Alice Johnson", "Designer", 55000);
stmt.run("Bob Brown", "Sales", 50000);
stmt.run("Charlie Davis", "Support", 45000);
stmt.run("Diana Evans", "HR", 70000);
stmt.run("Frank Green", "Marketing", 65000);
stmt.run("Grace Harris", "Finance", 75000);
stmt.run("Henry Jackson", "IT", 72000);
stmt.run("Ivy King", "Admin", 48000);
stmt.finalize();
});
app.get('/employees', (req, res) => {
db.all("SELECT * FROM employees", (err, rows) => {
if (err) {
return res.status(500).send(err);
}
res.json(rows);
});
});
app.post('/employees', (req, res) => {
const { name, position, salary } = req.body;
db.run("INSERT INTO employees (name, position, salary) VALUES (?, ?, ?)", [name, position, salary], function(err) {
if (err) {
return res.status(500).send(err);
}
res.json({ id: this.lastID, name, position, salary });
});
});
app.put('/employees/:id', (req, res) => {
const { id } = req.params;
const { name, position, salary } = req.body;
db.run("UPDATE employees SET name = ?, position = ?, salary = ? WHERE id = ?", [name, position, salary, id], function(err) {
if (err) {
return res.status(500).send(err);
}
res.json({ changes: this.changes });
});
});
// Add delete functionality
app.listen(3001, () => {
console.log('Server is running on port 3001');
});