-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (57 loc) · 1.73 KB
/
app.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
'use strict';
const express = require('express');
const path = require('path');
const logger = require('./lib/logger');
const expressWinston = require('express-winston');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const cors = require('cors');
const routes = require('./lib/routes');
const app = express();
function connectMongoose() {
const mongoose = require('mongoose');
mongoose.Promise = Promise;
return mongoose.connect('mongodb://' + process.env.MONGODB_HOST + ':' + process.env.MONGODB_PORT + '/' + process.env.MONGODB_DB, {useNewUrlParser: true});
}
function initialize() {
app.use(expressWinston.logger({
winstonInstance: logger,
expressFormat: true,
colorize: false,
meta: false,
statusLevels: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
Object.keys(routes).forEach((key) => {
app.use('/api', routes[key]);
});
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res, next) {
logger.error('handleError: ', err);
if (res.headersSent) {
return next(err);
}
let error = {};
error.status = err.status;
if (req.app.get('env') === 'development') {
error.message = err.message;
error.stack = err.stack;
}
return res.status(err.status || 500).json({
error
});
});
return app;
}
module.exports = {
initialize,
connectMongoose
};