-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.js
134 lines (109 loc) · 3.77 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"use strict";
var express = require('express'),
bodyParser = require('body-parser'),
fs = require('fs'),
app = express(),
customers = JSON.parse(fs.readFileSync('data/customers.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/page/:skip/:top', (req, res) => {
const topVal = req.params.top,
skipVal = req.params.skip,
skip = (isNaN(skipVal)) ? 0 : +skipVal;
let top = (isNaN(topVal)) ? 10 : skip + (+topVal);
if (top > customers.length) {
top = skip + (customers.length - skip);
}
console.log(`Skip: ${skip} Top: ${top}`);
var pagedCustomers = customers.slice(skip, top);
res.setHeader('X-InlineCount', customers.length);
res.json(pagedCustomers);
});
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(postedCustomer);
});
app.put('/api/customers/:id', (req, res) => {
let putCustomer = req.body;
let id = +req.params.id;
let status = false;
//Ensure state name is in sync with state abbreviation
const filteredStates = states.filter((state) => state.abbreviation === putCustomer.state.abbreviation);
if (filteredStates && filteredStates.length) {
putCustomer.state.name = filteredStates[0].name;
console.log('Updated putCustomer state to ' + putCustomer.state.name);
}
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/:id', function(req, res) {
let customerId = +req.params.id;
for (let cust of customers) {
if (cust.customerId === customerId) {
return res.json(cust);
}
}
res.json([]);
});
app.get('/api/states', (req, res) => {
res.json(states);
});
app.post('/api/auth/login', (req, res) => {
var userLogin = req.body;
//Add "real" auth here. Simulating it by returning a simple boolean.
res.json(true);
});
app.post('/api/auth/logout', (req, res) => {
res.json(true);
});
// 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.');
});