-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
80 lines (50 loc) · 1.64 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
const indexRouter = require('./routes/index');
const userRouter = require('./routes/user');
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
dotenv.config({path: './config/config.env'});
const mongoose = require('mongoose');
const express = require('express');
const app = express();
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const helmet = require('helmet');
const compression = require('compression');
const morgan = require('morgan');
const csrf = require('csurf');
const flash = require('connect-flash');
const store = new MongoDBStore({
uri: process.env.MONGO_URI,
collection: 'sessions'
});
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
const PORT = process.env.PORT || 3000;
app.set('view engine', 'ejs');
app.set('views', 'views');
app.use('/', express.static(__dirname + '/static/'));
app.use(session({secret: 'chahak', store: store, resave: true, saveUninitialized: true}));
const csrfProtection = csrf();
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'access.log'),
{ flags: 'a'}
);
app.use(csrfProtection);
app.use((req,res,next) => {
res.locals.csrfToken = req.csrfToken();
next();
});
app.use(helmet());
app.use(compression());
app.use(morgan('combined', { stream: accessLogStream}));
app.use(flash());
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true, useUnifiedTopology: true})
.then(result => {
app.listen(PORT);
})
.catch(err => {
console.log(err);
});
app.use(userRouter);
app.use(indexRouter);