-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (60 loc) · 1.8 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
64
65
66
67
68
69
70
import express from 'express';
import data from './src/data-source.js';
import bodyParser from 'body-parser';
import cors from 'cors';
const app = express();
app.use(cors({
origin: '*', // Update to the correct frontend origin
credentials: true
}));
app.use(bodyParser.json());
// Root route
app.get('/', (req, res) => {
res.send('Server is running');
});
// all records
app.get('/orders', function (req, res) {
res.json({ result: data, count:data.length });
});
// insert
app.post('/orders', function (req, res){
data.splice(0, 0, req.body.value);
res.status(200).send('Row Inserted');
});
// get specific records
app.get('/orders/:OrderID', (req, res) => {
const orderID = parseInt(req.params.OrderID, 10);
const order = data.find(d => d.OrderID === orderID);
if (order) {
res.json(order);
} else {
res.status(404).send('Order not found');
}
});
// Remove
app.delete('/orders/:OrderID', function (req, res) {
const orderID = parseInt(req.params.OrderID, 10);
const index = data.findIndex(x => x.OrderID === orderID);
if (index !== -1) {
data.splice(index, 1);
res.status(200).send('Row Deleted');
} else {
res.status(404).send('Order not found');
}
});
// Update
app.put('/orders/:OrderID', function (req, res) {
const orderID = parseInt(req.params.OrderID, 10);
const index = data.findIndex(x => x.OrderID === orderID);
if (index !== -1) {
// Assuming req.body.value contains the updated record with the same OrderID
data[index] = req.body.value;
res.status(200).send('Row Updated');
} else {
res.status(404).send('Order not found');
}
});
const port = xxxx; // Here xxxx denotes the port number.
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});