-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
59 lines (48 loc) · 1.66 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
// start using strict ES6
'using strict';
const express = require('express');
const app = express();
const logger = require('morgan');
const cookie_parser = require('cookie-parser');
const body_parser = require('body-parser');
const config = require('./config/default.js');
const favicon = require('serve-favicon');
const port = process.env.PORT || config.port;
const host = process.env.HOST || config.host;
// add a logger if env is not test
if (process.env.NODE_ENV !== 'test') {
app.use(logger(':method :status - :response-time ms - :url'));
}
// set json properties
app.set('json spaces', 2);
app.locals.pretty = true;
// set req and res properties
app.use((req, res, next) => {
req.accepts(['html', 'json']);
res.set('Content-type', 'application/json');
res.type('json');
next();
} );
// add a favicon
app.use(favicon(__dirname + '/data/favicon.png'));
// add cookie and body parsers
app.use(cookie_parser(config.cookie_secret));
app.use(body_parser.json());
app.use(body_parser.urlencoded({extended: true}));
// add routes
app.use('/', require('./routes/index.js'));
app.use('/users', require('./routes/user.js'));
app.use('/items', require('./routes/item.js'));
app.use('/reports', require('./routes/report.js'));
app.use('/auth', require('./routes/auth.js'));
app.use('/patients', require('./routes/patient.js'));
app.use('/search', require('./routes/search.js'));
// send 'bad request' if no route found
app.use('*', (req, res, next) => {
res.status(400).send({status: 400, message: 'Bad request'});
});
// start the app
app.listen(port, host, () => {
console.log('Listening on ' + host + ':' + port);
});
module.exports = app; // for use in testing