-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
101 lines (76 loc) · 2.54 KB
/
config.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
97
98
99
100
101
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const exphbs = require('express-handlebars');
const session = require('express-session');
const flash = require('connect-flash');
const path = require('path');
const passport = require('passport');
const LocalStatergy = require('passport-local').Strategy;
const mongo = require('mongodb');
const mongoose = require('mongoose');
const multer = require('multer');
const expressValidator = require('express-validator');
const methodOverride = require('method-override')
// using blueprint like structure
const librarian = require('./routes/librarian');
const users = require('./routes/user');
// Database Connection
mongoose.connect('mongodb://localhost/diGiLib',{ useNewUrlParser: true });
const db = mongoose.connection;
// Init Webapp
const webapp = express();
// View Engine
webapp.set('views',path.join(__dirname,'views'));
webapp.engine('handlebars',exphbs({defaultLayout:'layout(user)materialize'}));
webapp.set('view engine','handlebars');
// BodyParser Middleware
webapp.use(bodyParser.json());
webapp.use(bodyParser.urlencoded({extended: false}));
webapp.use(cookieParser());
webapp.use(express.static(path.join(__dirname,'client')));
// Method Override to support put and delete method
webapp.use(methodOverride());
// Express Session
webapp.use(session({
secret:'dacad7ad2a6480128619ee5e5c952da0',
saveUninitialized:true,
resave:true
}));
// Passport Initialization
webapp.use(passport.initialize());
webapp.use(passport.session());
webapp.use(expressValidator({
errorFormatter:function(param,msg,value){
var namespace = param.split('.'),
root = namespace.shift(),
formParm = root;
while(namespace.length){
formParm += '[' + namespace.shift() +']';
}
return {
param:formParm,
msg:msg,
value:value
};
}
}));
// Using Flash
webapp.use(flash());
webapp.use(function(req,res,next){
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
res.locals.msg = req.flash('msg');
next();
});
// Configuring route for routes
webapp.use('/librarian',librarian);
webapp.use('/users',users);
// Set Port
webapp.set('port', (process.env.PORT || 3000));
webapp.get('/',(req,res)=>{
res.render("welcome",{title:'Welcome digiLib',layout:'none'})
});
module.exports = { webapp };