-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
67 lines (58 loc) · 1.55 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
const express = require('express');
const session = require('express-session');
const { v4 } = require('uuid');
const path = require("path");
const app = express();
const PORT = 3000;
app.use(express.static(__dirname + '/assets'));
// Created session for 30 min
app.use(session({
genid: function(req){
return v4();
},
cookie: {expires: 1800000},
secret: 'secret_key',
name: 'uniqueSessionID',
saveUninitialized: false
}))
app.get("/", (req, res) => {
res.sendFile("index.html", { root: path.join(__dirname) });
});
app.get(`/blogs/:id`, (req, res) => {
// console.log(req.session);
console.log(req.path)
if(req.session.loggedIn){
if (req.params.id) {
res.sendFile(req.path, {root: path.join(__dirname) })
}
}else{
res.sendFile('login.html', {root: path.join(__dirname) })
}
});
app.get('/login', (req, res) => {
res.sendFile('login.html', { root: path.join(__dirname) })
})
app.post('/authenticate'
, express.urlencoded({ extended: true })
, (req, res, next) => {
if (req.body.username == 'test' && req.body.password == 'test') {
res.locals.username = req.body.username
next()
}
else
res.sendStatus(401)
}
, (req, res) => {
const header = req.header('Referer');
req.session.loggedIn = true
req.session.username = res.locals.username
// console.log(req.session)
if(header.includes('/blogs/')){
res.redirect(header);
}else{
res.redirect("/")
}
})
app.listen(PORT, (req, res) => {
console.log(`Your server is running on ${PORT}`)
});