-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (45 loc) · 1.4 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
const express = require('express');
var session = require('express-session');
const bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const hbs = require('express-handlebars');
const path = require('path');
const config = require('./config/config');
const routes = require('./routes/routes');
const { EDESTADDRREQ } = require('constants');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB, { useNewUrlParser: true})
.then(()=> {console.log('Connected to DB')},
err => {console.log('Error connecting to DB: '+ err)}
);
const app = express();
//app.use(express.static('static'));
app.use(session({
key: 'id',
secret: 'SuperSecureKey',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000
}
}));
//Create template engine
app.engine('hbs',hbs({extname: 'hbs',defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts/'}));
app.set('views',path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(cookieParser());
//remove previous cookies
console.log
app.use((req, res, next)=>{
if(req.cookies.id && !req.session.user){
res.clearCookie('id');
}
next();
});
app.use('/', routes);
const server = app.listen(config.app_port, ()=> {
console.log(`Server running on port ${config.app_port}`);
})