-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (81 loc) · 3 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
const express = require('express');
const app = express();
const http = require('http');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const validator = require('express-validator');
const session = require('express-session');
const mongoose = require('mongoose');
const flash = require('connect-flash');
const passport = require('passport');
const Helpers = require('./helpers');
const methodOverride = require('method-override');
const gate = require('app/helpers/gate');
const i18n = require("i18n");
const rememberLogin = require('app/http/middleware/rememberLogin');
const helmet = require('helmet');
const csrf = require('csurf');
const csrfErrorHandler = require('app/http/middleware/csrfErrorHandler');
const activeUser = require('app/http/middleware/activeUser');
module.exports = class Application {
constructor() {
this.setupExpress();
this.setMongoConnection();
this.setConfig();
this.setRouters();
}
setupExpress() {
const server = http.createServer(app);
server.listen(config.port , () => console.log(`Listening on port ${config.port}`));
}
setMongoConnection() {
mongoose.Promise = global.Promise;
mongoose.connect(config.database.url , { useNewUrlParser: true });
}
/**
* Express Config
*/
setConfig() {
require('app/passport/passport-local');
require('app/passport/passport-google');
require('app/passport/passport-jwt');
app.enable('trust proxy');
app.use(helmet());
app.use(express.static(config.layout.public_dir));
app.set('view engine', config.layout.view_engine);
app.set('views' , config.layout.view_dir);
app.use(config.layout.ejs.expressLayouts);
app.set("layout extractScripts", config.layout.ejs.extractScripts);
app.set("layout extractStyles", config.layout.ejs.extractStyles);
app.set("layout" , config.layout.ejs.master);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended : true }));
app.use(methodOverride('_method'));
app.use(validator());
app.use(session({...config.session}));
app.use(cookieParser(config.cookie_secretkey));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(rememberLogin.handle);
app.use(gate.middleware());
i18n.configure({
locales:['en', 'fa'],
directory: config.layout.locales_directory ,
defaultLocale : 'fa',
cookie : 'lang',
objectNotation: true,
});
app.use(i18n.init);
app.use((req , res , next) => {
app.locals = new Helpers(req, res).getObjects();
next();
});
}
setRouters() {
app.use(activeUser.handle);
app.use(require('app/routes/api'));
app.use(csrf({ cookie : true }) ,require('app/routes/web'));
app.use(csrfErrorHandler.handle);
}
}