forked from DanWahlin/Angular-JumpStart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (85 loc) · 2.81 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var express = require('express'),
bodyParser = require('body-parser'),
fs = require('fs'),
app = express(),
customers = JSON.parse(fs.readFileSync('data/customers.json', 'utf-8')),
orders = JSON.parse(fs.readFileSync('data/orders.json', 'utf-8')),
states = JSON.parse(fs.readFileSync('data/states.json', 'utf-8'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Would normally copy necessary scripts into src folder (via grunt/gulp) but serving
//node_modules directly to keep everything as simple as possible
app.use('/node_modules', express.static(__dirname + '/node_modules'));
//The src folder has our static resources (index.html, css, images)
app.use(express.static(__dirname + '/src'));
app.get('/api/customers', (req, res) => {
res.json(customers);
});
app.get('/api/customers/:id', (req, res) => {
let customerId = +req.params.id;
let selectedCustomer = {};
for (let customer of customers) {
if (customer.id === customerId) {
selectedCustomer = customer;
break;
}
}
res.json(selectedCustomer);
});
app.post('/api/customers', (req, res) => {
let postedCustomer = req.body;
let maxId = Math.max.apply(Math,customers.map((cust) => cust.id));
postedCustomer.id = ++maxId;
postedCustomer.gender = (postedCustomer.id % 2 === 0) ? 'female' : 'male';
customers.push(postedCustomer);
res.json({ status: true });
});
app.put('/api/customers/:id', (req, res) => {
let putCustomer = req.body;
let id = +req.params.id;
let status = false;
for (let i=0,len=customers.length;i<len;i++) {
if (customers[i].id === id) {
customers[i] = putCustomer;
status = true;
break;
}
}
res.json({ status: status });
});
app.delete('/api/customers/:id', function(req, res) {
let customerId = +req.params.id;
for (let i=0,len=customers.length;i<len;i++) {
if (customers[i].id === customerId) {
customers.splice(i,1);
break;
}
}
res.json({ status: true });
});
app.get('/api/orders', function(req, res) {
res.json(orders);
});
app.get('/api/orders/:id', function(req, res) {
let customerId = +req.params.id;
for (let order of orders) {
if (order.customerId === customerId) {
return res.json([ order ]);
}
}
res.json([]);
});
app.get('/api/states', (req, res) => {
res.json(states);
});
// redirect all others to the index (HTML5 history)
app.all('/*', function(req, res) {
res.sendFile(__dirname + '/src/index.html');
});
app.listen(3000);
console.log('Express listening on port 3000.');
//Open browser
var opn = require('opn');
opn('http://localhost:3000').then(() => {
console.log('Browser closed.');
});