-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·96 lines (74 loc) · 1.77 KB
/
index.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
import express from 'express';
import path from 'path';
import session from 'express-session';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
/**
* load configuration
*/
import configCommon from './src/Config/config.common.js';
import configRoutes from './src/Config/config.routes.js';
/**
* Load MiddleWares
*/
import RequestMiddleware from './src/Middlewares/RequestMiddleware.js';
/**
* initialize express app
* @type {[type]}
*/
const __dirname = path.resolve();
const app = express();
/**
* set the static path
*/
app.use(express.static('public'));
/**
* body parser
*/
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(bodyParser.raw());
/**
* set session
* @type {string}
*/
app.use(session({
// It holds the secret key for session
secret: configCommon.session.secret,
// Forces the session to be saved
// back to the session store
resave: true,
// Forces a session that is "uninitialized"
// to be saved to the store
saveUninitialized: true,
cookie:{
maxAge: configCommon.session.sessionTime
}
}));
/**
* set Request Middleware
*/
app.use(RequestMiddleware.callMiddleWare)
/**
* connect the DB
*/
mongoose.connect(configCommon.db.url, configCommon.db.options);
/**
* set routers dynamically
*/
configRoutes.forEach((route) => {
const middlewares = [];
route.middlewares.forEach((middleware) => {
middlewares.push(middleware.callMiddleWare)
})
app[route.method](route.path, middlewares,route.module.callModule);
})
/**
* listen the server
* @type {string}
*/
const server = app.listen(configCommon.server.port, function () {
const host = server.address().address
const port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})