-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (61 loc) · 1.78 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require('express');
const dotenv = require('dotenv');
const exphbs = require('express-handlebars');
const session = require('express-session');
const path = require('path');
const pgStore = require('connect-pg-simple')(session);
const csrf = require('csurf');
const flash = require('connect-flash');
const pool = require('./config/db');
const db = require('./models/index.model');
//Initial env variables
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({
store: new pgStore({
pool: pool,
tableName: 'user_session',
}),
secret: 'my secret value',
resave: false,
saveUninitialized: false
}));
app.use(flash());
//Initialize template engine (handlebars)
app.engine('.hbs', exphbs.engine({ extname: '.hbs' }));
app.set('view engine', '.hbs');
//Static File
app.use(express.static(path.join(__dirname, 'public')));
//Initialize routes
app.use('/diary', require('./routes/diary.route'));
app.use(csrf());
app.use((req, res, next) => {
res.locals.csrfToken = req.csrfToken();
next();
});
app.use('/auth', require('./routes/auth.route'));
app.use('/user', require('./routes/user.route'));
app.use('/', async(req, res) => {
try {
if (req.session.isLogged) {
return res.redirect('/diary/my');
}
res.redirect('/auth/login');
} catch (error) {
console.log(error);
}
})
const PORT = process.env.PORT || 10000
const start = async () => {
try {
const connect = await db.sequelize.sync();
app.listen(PORT, () => {
console.log(`\n🥳 🥳 🥳 🤭\nhttp://localhost:${PORT}/diary/my\nServer is running on port ${PORT}`)
});
} catch (error) {
console.log(error);
}
}
start();