-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
96 lines (80 loc) · 3.01 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var express = require('express');
var mongoose = require('mongoose');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var compress = require('compression');
var methodOverride = require('method-override');
var cors = require('cors');
var httpStatus = require('http-status');
var expressValidation = require('express-validation');
var helmet = require('helmet');
var config = require('./config/config');
var APIError = require('./helpers/APIError');
var path = require('path');
var appRoot = require('app-root-path');
var favicon = require('serve-favicon');
var debug = require('debug')('api:app');
var swaggerUi = require('swagger-ui-express');
var swaggerDocument = require('./swagger.json');
const app = express();
debug('NODE_ENV=%s', config.env)
if (config.env === 'development') {
app.use(morgan('dev'));
} else {
app.use(morgan('combined'));
}
// connect mongodb by setting mongoose
const mongoUrl = 'mongodb://' + config.mongo.host + ':' + config.mongo.port + '/' + config.env;
if (config.env === 'development') {
mongoose.set('debug', config.mongooseDebug);
}
mongoose.connect(mongoUrl, { useNewUrlParser: true });
mongoose.connection.on('connected', function () {
debug('Mongoose default connection open to ' + mongoUrl);
});
// parse body params and attache them to req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(compress());
app.use(methodOverride());
// secure apps by setting various HTTP headers
app.use(helmet());
// enable CORS - Cross Origin Resource Sharing
app.use(cors());
// Middlewares
app.use(express.static(path.join(appRoot.path, 'public')));
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api', require('./routes/index.route'));
app.use('/v1', require('./routes/v1/index.route'));
app.get('*', (req, res) => {
res.sendFile(path.join(appRoot.path, 'public/index.html'));
});
// if error is not an instanceOf APIError, convert it.
app.use((err, req, res, next) => {
if (err instanceof expressValidation.ValidationError) {
// validation error contains errors which is an array of error each containing message[]
const unifiedErrorMessage = err.errors.map(error => error.messages.join('. ')).join(' and ');
const error = new APIError(unifiedErrorMessage, err.status, true);
return next(error);
} else if (!(err instanceof APIError)) {
const apiError = new APIError(err.message, err.status, err.isPublic);
return next(apiError);
}
return next(err);
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new APIError('API not found', httpStatus.NOT_FOUND);
return next(err);
});
// error handler, send stacktrace only during development
app.use((err, req, res, next) => {
debug(err.stack)
return res.status(err.status).json({
message: err.isPublic ? err.message : httpStatus[err.status],
stack: config.env === 'development' ? err.stack : {}
})
});
module.exports = app;