-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (52 loc) · 1.74 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
const express = require('express'),
server = express(),
fs = require('fs'),
orderData = require('./orders');
server.set('port', process.env.PORT || 3000);
server.get('/',(request,response)=>{
response.send('Welcome to our simple online order managing web app!');
});
//Adding the /orders code:
server.get('/orders',(request,response)=>{
response.json(orderData);
});
//Adding the /neworder code:
server.post('/neworder', express.json(), (request,response)=>{
orderData.orders.push(request.body);
fs.writeFileSync('orders.json', JSON.stringify(orderData));
response.send("Success")
console.log("Success");
});
//Adding the /update/:id code:
server.put('/update/:id', express.text({type: '*/*'}), (request,response)=>{
var items = orderData.orders
items.forEach(function(o) {
console.log(o)
if (o.id == request.params.id){
console.log('Modifying order!')
o.state = request.body;
}
});
fs.writeFileSync('orders.json', JSON.stringify(orderData));
response.send('Success');
console.log('Success');
});
//Adding the /delete/:id code:
server.delete('/delete/:id', (request,response)=>{
var items = orderData.orders
var newData = {"orders": []}
items.forEach(function(o) {
console.log(o)
if (o.id == request.params.id){
console.log('Deleting order!')
} else{
newData.orders.push(o)
}
});
fs.writeFileSync('orders.json', JSON.stringify(newData));
response.send('Success');
console.log('Success');
});
server.listen(3000,()=>{
console.log('Express server started at port 3000');
});