-
Notifications
You must be signed in to change notification settings - Fork 2
/
dev_server.js
85 lines (66 loc) · 1.96 KB
/
dev_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
'use strict';
let express = require('express');
let logger = require('morgan');
let app = new express();
// ## Middleware
app.use(logger('dev'));
app.use(express.static(`${__dirname}/public`));
// Dev middleware:
let webpack = require('webpack');
let config = require('./webpack.dev.config.js');
let webpackDevMiddleware = require('webpack-dev-middleware');
let webpackHotMiddleware = require('webpack-hot-middleware');
let compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, { publicPath: config.output.publicPath }));
app.use(webpackHotMiddleware(compiler));
// Mock APIs
let _ = require('lodash');
let moment = require('moment');
let data = require('./dev_data.json');
// Calculates event date based on an offset, so that we can test easier.
data.map(event => {
event.date = moment()
.date(event.date.day)
.add(event.date.month, 'months')
.format('YYYY-MM-DD');
delete event.offset;
return event;
})
function getEvents(start, end, type) {
var events = data;
if (type) {
events = _.filter(events, { type: type });
}
if (start && end) {
events = _.filter(events, event => {
return event.date >= start && event.date <= end;
});
}
return events;
}
function getEvent(id) {
return _.find(data, { id: id });
}
// List all events for user
app.get('/calendar/events', (req, resp) => {
let start = req.query.start;
let end = req.query.end;
let events = getEvents(start, end);
resp.json(events);
});
// List all events of type for user
app.get('/calendar/events/:type', (req, resp) => {
let type = req.params.type;
let start = req.query.start;
let end = req.query.end;
let events = getEvents(start, end, type);
resp.json(events);
});
// Get a single event
app.get('/events/:id', (req, resp) => {
let id = req.params.id;
let event = getEvent(id);
resp.json(event);
});
let port = 3333 || process.env.port;
app.listen(port, err => console.log(`Listening on ${port}`));