-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
96 lines (75 loc) · 2.39 KB
/
server.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
const express = require('express');
const dotenv = require('dotenv').config();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const path = require('path');
const session = require('express-session');
const validator = require('express-validator');
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
const passport = require('passport');
const flash = require('connect-flash');
const _ = require('lowdash');
var routes = require('./routes/index');
var users = require('./routes/users');
var dashboard = require('./routes/dashboard');
var event = require('./routes/events');
// const { config } = require('dotenv/types');
const app = express();
var port = process.env.PORT || 8001 ;
mongoose.Promise = global.Promise;
const mongo_uri = process.env.mongo_uri;
const connect = mongoose.connect(mongo_uri, { useUnifiedTopology: true, useNewUrlParser: true });
connect.then((db) =>
{
console.log("Database Connected Successfully");
}, (err) =>
{
console.log("Error occur while connecting ", err);
})
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(cookieParser());
app.use(validator());
app.use(session({
secret: 'thisisasecretkey',
resave: true,
saveInitialized:true,
store:new MongoStore({mongooseConnection:mongoose.connection})
}))
app.use(passport.initialize());
app.use(passport.session());
app.use(validator({
errorFormatter: function(param, msg, value){
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while (namespace.length){
formParam += '[' +namespace.shift() + ']';
}
return{
param : formParam,
msg : msg,
value : value
};
}
}));
app.use(flash());
app.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;
next();
});
app.use('/', routes);
app.use('/users', users);
app.use('/dashboard', dashboard);
app.use('/event', event);
app.listen(port, () => {
console.log(`Server is listening on ${port}`);
})