-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (51 loc) · 1.4 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const helmet = require('helmet');
const api = require('./routes/api');
const views = require('./routes/views')
const port = process.env.PORT || 5000;
const app = express();
const http = require('http').Server(app);
var sessionSecret = process.env.SESSION_SECRET || 'TEST_SECRET';
app.use(helmet());
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static('views'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(logError);
app.use(session({
key: 'userId',
secret: sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 604800000,
}
}));
app.use((req, res, next) => {
if (req.cookies.userId && !req.session.user) {
res.clearCookie('userId');
}
next();
});
app.use(compression());
app.use('/api', api);
app.use('/', views);
app.use(function(req, res) {
res.status(400);
res.render("notFound.html");
});
function logError(error, req, res, next) {
console.log(error);
next(error);
};
http.listen(port, function() {
console.log("Listening on port " + port);
});
module.exports = app;