-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (57 loc) · 1.64 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
require('dotenv').config()
const express=require('express');
const compression = require('compression')
const mongoose=require('mongoose')
const session = require('express-session')
const bodyParser = require('body-parser');
const app=express();
//using gzip to reduce body sizes
app.use(compression())
//setting bodyparser
app.use(bodyParser.urlencoded({limit: '50mb', 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: {
//todo:remeber to set this to true when in production
secure: false,
maxAge:60*60*1000 //1 hour
}
}))
//importing routes
const routes=require('./routes/routes');
app.use('/',routes);
//404 page
app.use((req, res) => {
return res.render('404error.ejs',{
isLogged:req.session.isLogged
})
})
var port=process.env.PORT || 3000;
app.listen(port,()=>{
console.log("Server started.");
});
// const privateKey= fs.readFileSync('tech-priv.pem');
// const certificate = fs.readFileSync('tech-pub.pem')
// https.createServer({
// cert:certificate,
// key:privateKey
// },app).listen(port);