forked from gocd-demo/node-bulletin-board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (57 loc) · 2.03 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
const express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler'),
morgan = require('morgan'),
routes = require('./backend'),
UUIDGenerator = require('uuid/v4'),
eventService = new require('./backend/event_service')(UUIDGenerator);
var app = module.exports = express();
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use('/bulletin-board', express.static(__dirname + '/'));
app.use('/', express.static(__dirname + '/'));
var env = process.env.NODE_ENV;
if ('development' == env) {
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
}
if ('production' == app.get('env')) {
app.use(errorHandler());
}
app.get('/bulletin-board', routes.index);
app.get('/', routes.index);
app.get('/bulletin-board/api/events', function (req, res) {
res.json(eventService.getAllEvents())
});
app.get('/api/events', function (req, res) {
res.json(eventService.getAllEvents())
});
app.post('/bulletin-board/api/event', function (req, res) {
eventService.addEvent(req.body);
res.json(eventService.getAllEvents());
});
app.post('/api/event', function (req, res) {
eventService.addEvent(req.body);
res.json(eventService.getAllEvents());
});
app.delete('/bulletin-board/api/event/:eventId', function (req, res) {
const result = eventService.deleteEvent(req.params.eventId);
res.json({message: result ? 'Event successfully deleted.' : `Event with id ${req.params.eventId} does not exist.`});
});
app.delete('/api/event/:eventId', function (req, res) {
const result = eventService.deleteEvent(req.params.eventId);
res.json({message: result ? 'Event successfully deleted.' : `Event with id ${req.params.eventId} does not exist.`});
});
const server = app.listen(8080);
app.shutdown = function () {
console.log("Shutting down...")
server.close();
};
console.log('Server started on port 8080...');