-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·81 lines (70 loc) · 2.46 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
const config = require('./config/index');
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
// const path = require('path');
const methodOverride = require('method-override');
const logger = require('morgan');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const compress = require('compression');
const expressValidation = require('express-validation');
const httpStatus = require('http-status');
const passport = require('passport');
const indexRoutes = require('./routes/index');
const APIError = require('./helpers/APIError');
const app = express();
if (config.env === 'development') {
app.use(logger('dev'));
}
mongoose.Promise = global.Promise;
mongoose
.connect(config.mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => console.log('Connected to Mongoose'))
.catch(err => console.log(err));
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
require('./config/passport')(passport);
app.use(compress());
app.use(methodOverride());
app.use(helmet());
app.use(cors());
// app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', indexRoutes);
// 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) =>
res.status(err.status).json({
message: err.isPublic ? err.message : httpStatus[err.status],
stack: config.env === 'development' ? err.stack : {}
})
);
app.listen(config.port, () => {
console.info(`server started on port ${config.port} (${config.env})`); // eslint-disable-line no-console
});
module.exports = app;