forked from UofTTeam2/TravIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (73 loc) · 2.98 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
75
76
77
78
79
80
81
82
// Project: TravIs
// File Created: 2023-10-17
// Desc: Main server file for the application.
// This file will be used to start the server and connect to the database.
// The server will be listening on port 3001.
//==============================================================
// Dependencies
// =============================================================
const express = require('express');
const sequelize = require('./config/connection');
const session = require('express-session');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const path = require('path');
const exphbs = require('express-handlebars');
// load env variables
require('dotenv').config();
//==============================================================
//importing routes, Session model and custom middlewares
// =============================================================
const routes = require('./controllers');
const Session = require('./models/Session');
const helpers = require('./utils/helpers');
//initializes handlebars template engine
const hbs = exphbs.create({
helpers,
runtimeOptions: {
allowProtoPropertiesByDefault: true,
allowProtoMethodsByDefault: true,
},
});
// import dataParser middleware
const dataParser = require('./utils/dataParser');
//==============================================================
//defines express application and PORT
// =============================================================
const app = express();
const PORT = process.env.PORT || 3001;
//==============================================================
// Access the session secret from the environment variables
// =============================================================
const sessionSecret = process.env.SESSION_SECRET;
//defining express session
const sess = {
secret: sessionSecret,
cookie: {
// Session will automatically expire after one hour
expires: 60 * 60 * 1000,
},
resave: false,
saveUninitialized: true,
store: new SequelizeStore({
db: sequelize,
table: 'sessions', // Name of the database table to store sessions
}),
};
app.use(session(sess));
//==============================================================
// Registering handlebars as the template engine of choice
// =============================================================
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
//==============================================================
// Sets up the custom middleware & Express app to handle data parsing
// =============================================================
app.use(dataParser);
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
//==============================================================
// sync sequelize models to the database, then start running the server
// =============================================================
sequelize.sync({force: false}).then(() => {
app.listen(PORT, () => console.log(`App listening on port ${PORT}!`));
});