-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (57 loc) · 2.39 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
const helmet = require('helmet');
const morgan = require('morgan');
const express = require('express');
const mongoose = require('mongoose');
const bluebird = require('bluebird');
const swaggerUi = require('swagger-ui-express');
const config = require('./config/config');
const swaggerSpec = require('./swagger');
const { customError, requiredParam, notification, validateParams, validateQuery, validateBody } = require('./utilities');
const responseWrapper = require('./middlewares/responseWrapper');
const { catchAsyncErrors } = require('./middlewares/error');
// Set Global Variables
global.Promise = bluebird;
global.empty = require('is-empty');
global.ValidateParams = validateParams
global.ValidateQuery = validateQuery
global.ValidateBody = validateBody;
global.CatchAsyncErrors = catchAsyncErrors;
global.CustomError = customError;
global.RequiredParam = requiredParam;
global.SendNotifications = notification({ notifications: config.notifications });
// Configure mongoose
mongoose.Promise = bluebird;
mongoose.connect(config.MONGODB_URI, {useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true});
global.models = require('./models'); // Push Models to global
// To Print Unhandled Error
process.on('unhandledRejection', (error) => {
global.SendNotifications.sendSlackNotification('', `Error on HR Api System \n \`\`\`${error.stack}\`\`\``).catch(console.log);
console.error('unhandledRejection', error.message);
});
// Create new express app and get the port
const app = express();
const port = config.PORT;
// Swagger
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Configure the app Middlewares
app.use(helmet.hidePoweredBy({ setTo: 'PHP/5.4.0'}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('tiny'));
// Import Routes
const indexRoute = require('./routes');
const userRoute = require('./routes/user');
const benefitRoute = require('./routes/benefit');
// Route Middlewares
app.use('/', indexRoute);
app.use('/user', userRoute);
app.use('/benefit', benefitRoute);
// The Catch all Not found route
app.all('*', (req, res, next) => next(new CustomError('Not Found', 404)));
// Error handler
app.use(responseWrapper.errorHandler);
// Data handler
app.use(responseWrapper.dataHandler);
// Bind the app to the port
app.listen(port, () => console.log(`Server Up and Running \n=> http://localhost:${config.PORT}`));
module.exports = app;