-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
69 lines (54 loc) · 1.84 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
const express = require('express');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const path = require('path');
const csrf = require('csurf');
const flash = require('connect-flash');
// const dbConnect = require('./config/database');
const webRoutes = require('./routes/web');
const app = express();
app.set('view engine','ejs');
app.set('views','resource/views');
app.use(express.json());
app.use(express.urlencoded({extended:false}));
app.use(express.static(path.join(__dirname,'public')));
app.use(cookieParser());
app.use(csrf({ cookie: true }));
app.use(session({
name: "my-session-name",
secret: 'keyboard cat',
resave: true,
saveUninitialized: false,
cookie: { /*maxAge: 60000*/ },
// cookie: { secure: true, httpOnly: true }
}));
app.use(flash());
app.use((req, resp, next) => {
resp.locals.csrfToken = req.csrfToken();
resp.locals.auth = req.session.auth? req.session.auth : false;
resp.locals.username = req.session.username? req.session.username : '';
resp.locals.roles = req.session.roles?req.session.roles:'';
resp.locals.routeName = req.originalUrl.split('/')[1];
resp.locals.message = req.flash();
next();
});
app.use(webRoutes);
app.use((req,resp,next) => {
// resp.status(404).sendFile(path.join(__dirname,'views','errors','404.html'));
resp.status(404).render('errors/404');
});
app.use((error,req,resp,next) => {
// resp.status(404).sendFile(path.join(__dirname,'views','errors','404.html'));
resp.status(500).render('errors/500',{
error: error
});
});
app.listen(8000);
// const http = require('http');
//Core way of http server initialization
// const test = (req,res) => {
// res.write("<h1>Hi I am here</h1>");
// return res.end();
// }
// const server = http.createServer(test);
// server.listen(8000);