-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
130 lines (104 loc) · 3.36 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
const path = require('path');
const fs = require('fs');
const Express = require('express');
const bodyParser = require('body-parser');
const App = Express();
const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath()
App.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', req.headers.origin||'*');
res.header('Access-Control-Allow-Methods', 'GET,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials', true);
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
return next();
});
App.use(bodyParser.json());
App.use('/', Express.static(path.resolve('./public')))
App.get('/swagger', (req, res) => res.sendFile(path.resolve('./public','swagger.html')));
App.use('/swagger/dist', Express.static(swaggerUiAssetPath));
//API
const ApiRouter = Express.Router({mergeParams: true});
ApiRouter.use(async (req, res, next) => {
//Randomly send a 500 error
if (Math.random() < 0.005) {
return res.status(500).send("Unknown Error")
}
next();
})
const SORT_FUNCS = {
'survey_date': (a,b) => (
(new Date(a.survey_date)).getTime() - (new Date(b.survey_date)).getTime()
),
'full_name': (a,b) => (
a.full_name.localeCompare(b.full_name)
),
'email': (a,b) => (
a.email.localeCompare(b.email)
)
}
//
// Get All users
//
ApiRouter.route('/users')
.get(async (req, res) => {
let users = require('./src/server/data/users.json');
const limit = req.query.limit||20;
const page = Math.min(Math.max((req.query.page||1) - 1, 0), Math.ceil(users.length / limit)-1);
if (req.query.sort && SORT_FUNCS[req.query.sort]) {
users = users.sort(SORT_FUNCS[req.query.sort]);
}
if (req.query.order && req.query.order.toLowerCase()==='desc') {
users = users.reverse();
}
res.status(200).send({
meta: {
pages: Math.ceil(users.length / limit)-1,
page: page+1,
count: users.length,
limit: limit
},
data: users.slice(Number(page*limit), Number(page*limit) + Number(limit))
});
})
.all((req,res)=>res.status(405).send("405 Method Not Allowed"));
//
// Get Individual user
//
ApiRouter.route('/users/:id(\\d+)/')
.get(async (req, res) => {
const users = require('./src/server/data/users.json');
const user = users.find(u=>Number(u.id)===Number(req.params.id));
if (!user) {
return res.status(404).send({
code: 404,
message: `User ${req.params.id} does not exist`,
fields: "id"
})
}
res.status(200).send(user);
})
.all((req,res)=>res.status(405).send("405 Method Not Allowed"));
ApiRouter.route('/users/:id(\\d+)/info')
.get(async (req, res) => {
const user_info = require('./src/server/data/user_info.json');
const user = user_info.find(u=>Number(u.user_id)===Number(req.params.id));
if (!user) {
return res.status(404).send({
code: 404,
message: `User ${req.params.id} does not exist`,
fields: "user_id"
})
}
res.status(200).send(user);
})
.all((req,res)=>res.status(405).send("405 Method Not Allowed"));
App.use('/api', ApiRouter)
App.all('*', (req, res) => res.status(404).send({
code: 404,
message: "Page not found",
fields: null
}))
const SERVER_PORT = process.env.SERVER_PORT || 3000;
App.listen(SERVER_PORT, () => console.log(`Listening on ${SERVER_PORT}`))