-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (42 loc) · 1.45 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
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const compression = require('compression');
const passport = require('passport');
const httpStatus = require('http-status');
const config = require('./src/config/config');
const morgan = require('./src/config/morgan');
const authLimiter = require('./src/middlewares/rate_limit');
const routes = require('./src/routes/v1/index');
const { errorConverter, errorHandler } = require('./src/middlewares/error');
const { jwtStrategy, facebookStrategy, googleStrategy } = require('./src/config/passport');
const ApiError = require('./src/utils/api_error');
const app = express();
if (config.env !== 'test') {
app.use(morgan.successHandler);
app.use(morgan.errorHandler);
}
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(xss());
app.use(mongoSanitize());
app.use(compression());
app.use(cors());
app.options('*', cors());
app.use(passport.initialize());
passport.use('jwt', jwtStrategy);
passport.use('facebook', facebookStrategy);
passport.use('google', googleStrategy);
if (config.env === 'production') {
app.use('/v1/auth', authLimiter);
}
app.use('/v1', routes);
app.use((req, res, next) => {
next(new ApiError(httpStatus.NOT_FOUND, 'Not Found!!'));
});
app.use(errorConverter);
app.use(errorHandler);
module.exports = app;