-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-server.js
65 lines (51 loc) · 1.63 KB
/
json-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
/* global require, console */
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router(require('./db.js')());
const middlewares = jsonServer.defaults();
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);
server.use(function(req, res, next) {
res.header('content-type', 'application/vnd.api+json');
next();
});
router.render = function(req, res) {
let { data } = res.locals.data;
const id = req.query.rental_id;
let date = new Date();
// generate data
switch (id) {
case '832':
data[0].attributes.availability = `11000${data[0].attributes.availability}`;
// data starting from 3 days ago
date.setDate(date.getDate() - 3);
break;
case '833':
data[0].attributes.availability = `00000${data[0].attributes.availability}`;
// data starting from 3 days ago
date.setDate(date.getDate() - 3);
break;
case '834':
data[0].attributes.availability = `11111${data[0].attributes.availability}`;
break;
default:
data[0].attributes.availability = `00000${data[0].attributes.availability}`;
break;
}
const options = {
year: 'numeric',
month: '2-digit',
day: '2-digit'
};
const startDate = date.toLocaleString('en', options).split('/');
const [month, day, year] = startDate;
data[0].attributes.start_date = `${year}-${month}-${day}`;
data[0].id = id;
// merge data
res.locals.data.data = data;
res.jsonp(res.locals.data);
};
server.use('/api/v2/public', router);
server.listen(3000, function() {
console.log('JSON Server is running');
});