-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (62 loc) · 1.99 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
71
72
73
74
75
76
var http = require('http');
var uuid = require('node-uuid');
var serveStatic = require('node-static');
var fileServer = new serveStatic.Server('./public');
var express = require('express')
var bodyParser = require('body-parser')
var customers = [
{name: 'William Shakespeare', product: {name:'Grammatical advice'}, id: uuid.v4(), joinedTime: new Date().toString()},
{name: 'Sherlock Holmes', product: {name:'Magnifying glass repair'}, id: uuid.v4(), joinedTime: new Date().toString()},
{name: 'Allan Turing', product: {name:'Cryptography advice'}, id: uuid.v4(), joinedTime: new Date().toString()},
]
var servedCustomers = [
];
function serveCustomer(id){
customers = customers.filter(function(customer){
if(customer.id == id){
customer.status = 'served';
servedCustomers.push(customer);
return false;
}else{
return true;
}
})
}
function addCustomer(customer){
customer.id = uuid.v4();
customers.push(customer);
}
function removeCustomer(targetCustomerId){
customers = customers.filter(function(customer){
return customer.id != targetCustomerId;
})
}
var app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.get('/api/customers', function(req,res){
res.send(customers);
})
app.get('/api/customers/served', function(req,res){
res.send(servedCustomers);
})
app.post('/api/customer/add', function(req,res){
addCustomer(req.body);
res.end('Customer was added!');
});
app.post('/api/customer/serve', function(req,res){
serveCustomer(req.body.id);
res.end('Customer was served!');
});
app.delete('/api/customer/remove', function(req,res){
removeCustomer(req.query.id);
res.end('Customer was removed!');
});
app.use(function (req, res) {
req.addListener('end', function () {
fileServer.serve(req, res);
}).resume();
})
app.listen(1337)
console.log('Server is running @ 127.0.0.1:1337...');
console.log('Good luck!');