-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
77 lines (68 loc) · 2.49 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
73
74
75
76
77
//importing important modules
const path = require('path');
const express = require('express');
const app = express();
require('dotenv').config();//adding dotenv- loading environmental variable
require('express-async-errors');//handle async await error
const bodyParser = require('body-parser');
const db = require('./db');// database and connection details
const session = require('express-session'); // session-cookie managing tools
const MongodbStore = require('connect-mongodb-session')(session); // modules storing session to mongodb
const flash = require('connect-flash');
const csurf = require('csurf');
const csurfProtection = csurf();
const helmet = require('helmet');
const compression = require('compression');
const breadcrumbs = require('./component/utils/breadcrumbs');
app.use(breadcrumbs.init());
require('./config/multer')(app);//configure and initiation of multer
const morgan = require('morgan');
const logger = require('./config/winston'); // winston configuration
const accessController = require('./component/accessController/accessController');// top level routes
//database link
const dbLink = db.url;// our mongodb database link
//handling uncaught promise rejection
process.on('unhandledRejection', ex =>{
throw ex;
});
// integrating morgan for logging http request
app.use(morgan('combined', { stream: logger.stream.write }));
// Setting template engine, public folders
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/assets/images', express.static(path.join(__dirname, 'assets/images')));
//helmet
app.use(helmet());
app.use(compression());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: true
}))
// parse application/json
app.use(bodyParser.json())
// storing session into mongodb
const store = new MongodbStore({
uri: dbLink,
collection: 'sessions'
});
//session middleware function to handle session
app.use(session({
secret: 'Hello why when',
resave: false,
saveUninitialized: false,
store
}));
app.use(flash()); //flash initialization after session
app.use(csurfProtection); // csurf is middled
app.use((req, res, next) => {// local values of app are set
app.locals.isAuthenticated = req.session.isLoggedIn;
app.locals.role = req.session.user ? req.session.user.role : null;
app.locals.csrfToken = req.csrfToken();
next();
})
// Set Breadcrumbs home information
app.use(breadcrumbs.setHome());
//top level routes
accessController(app);
//connect
db.mongooseConnect(app);