-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
server.js
150 lines (125 loc) · 4.25 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
var express = require('express'),
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')),
inContainer = process.env.CONTAINER,
inAzure = process.env.WEBSITE_RESOURCE_GROUP,
port = process.env.PORT || 8080;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//CORS
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Headers", "Origin, Authorization, X-Requested-With, X-XSRF-TOKEN, X-InlineCount, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
next();
});
//The dist folder has our static resources (index.html, css, images)
if (!inContainer) {
app.use(express.static(__dirname + '/dist'));
console.log(__dirname);
}
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 = null;
for (let customer of customers) {
if (customer.id === customerId) {
// found customer to create one to send
selectedCustomer = {};
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);
});
if (!inContainer) {
// redirect all others to the index (HTML5 history)
app.all('/*', function(req, res) {
res.sendFile(__dirname + '/dist/index.html');
});
}
app.listen(port);
console.log('Express listening on port ' + port);
// //Open browser
if (!inContainer && !inAzure) {
var opn = require('opn');
opn('http://localhost:' + port).then(() => {
console.log('Browser closed.');
});
}