-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (66 loc) · 2.16 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
require('dotenv').config({ path: '.env' });
const express = require('express');
const path = require('path');
const expressSession = require('express-session');
const pgSession = require('connect-pg-simple')(expressSession);
const pgCon = require('pg')
const pool = new pgCon.Pool({
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
})
const app = express();
app.listen(process.env.PORT, '0.0.0.0', () => {
console.log(`Server listening on the port::${process.env.PORT}`);
});
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const store = new pgSession({
pool: pool,
tableName: 'session'
})
app.use(expressSession({
store: store, // comment to use the in-memory store
secret: process.env.COOKIE,
resave: true,
saveUninitialized: false,
unset: 'destroy',
secure: false,
cookie: {
maxAge: 60 * 60 * 1000,
},
}));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'));
});
app.post('/login', (req, res) => {
try {
if (req.session.username) {
console.log("user session already stored :)")
return res.status(200).send({ success: "user session was already stored :)" })
}
else if (req.body.username) {
req.session.username = req.body.username + Date.now()
console.log("user session now stored :)")
return res.status(200).send({ success: "a new session was created" })
}
else {
console.log("user session not stored or empty")
return res.status(200).send({ error: "user session not stored or empty" })
}
} catch (error) {
console.error(`/login : ${error}`)
res.json({ error: 'error during login' })
}
});
app.get('/session_table', async (req, res) => {
try {
const result = await pool.query('SELECT * FROM session')
res.json(result.rows)
} catch (error) {
console.error(`/session_table : ${error}`)
res.json({ error: 'error retrieving session table' })
}
});