-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·60 lines (46 loc) · 1.42 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
// const createError = require('http-errors');
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import httpStatus from 'http-status';
import mongoSanitize from 'express-mongo-sanitize';
import passport from 'passport';
import apiRoutes from './src/routes/api.js';
import ApiError from './src/utils/ApiError.js';
import {error} from './src/middlewares/index.js';
import {logger, config, JwtStrategy } from './src/config/index.js';
// const oldSpawn = childProcess.spawn;
const app = express();
// mongo config
import './src/utils/db_init.js';
import Admin from './src/models/admin.models.js';
// parse json body request
app.use(cors());
app.use(express.json());
// sanitize request data
// app.use(xss());
app.use(mongoSanitize());
// jwt auth
app.use(passport.initialize());
passport.use('jwt', JwtStrategy);
passport.use(Admin.createStrategy());
if (app.get('env') === 'development') {
app.use(morgan('tiny'));
}
// routing lvl middleware
app.use('/api', apiRoutes);
app.use((req, res, next) => {
next(httpStatus.NOT_FOUND);
});
// send back a 404 error for any unknown api request
app.use((req, res, next) => {
next(new ApiError(httpStatus.NOT_FOUND, 'Not found'));
});
// convert error to ApiError, if needed
app.use(error.errorConverter);
// handle error
app.use(error.errorHandler);
app.listen(config.port, () => {
logger.info(`Listening to port ${config.port}`);
});
export default app;