-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (44 loc) · 1.28 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
require('dotenv').config()
const express = require('express');
const app = express();
const mongoose=require('mongoose')
const session = require('express-session')
const bodyParser = require('body-parser');
//setting bodyparser
app.use(bodyParser.urlencoded({ extended: true }));
//Serving Static files
app.use(express.static('public'));
//Ejs initialization
app.set('view engine', 'ejs');
//Mongoose connection
mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, }, () => {
console.log("Connected to database");
});
//for storing sessions
const mongoDBStore = require('connect-mongodb-session')(session);
const store = new mongoDBStore({
uri: process.env.MONGO_URL,
collection: 'sessions'
})
//session intialization
app.set('trust proxy', 1)
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: store,
cookie: {
//remeber to set this to true when in production
secure: false,
maxAge: 60 * 60 * 1000 //1 hour
}
}))
const routes = require('./routes/routes')
app.use('/', routes)
//404 page
app.use((req, res) => {
res.send('404 error');
})
app.listen(process.env.PORT || 3000, () => {
console.log("Server started")
})