-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (61 loc) · 2.69 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
// Local Modules
const routes = require('./controllers');
const helpers = require('./utils/helpers');
// Third-Party Modules
const path = require('path');
const express = require('express');
const session = require('express-session');
const exphbs = require('express-handlebars');
const sequelize = require('./config/connection');
// Create a new sequelize store using the express-session package
const SequelizeStore = require('connect-session-sequelize')(session.Store);
// Initialize an instance of Express.js
const app = express();
// Specify on which port the Express.js server will run
const PORT = process.env.PORT || 3001;
// Set up Handlebars.js as the default engine with custom helpers
const hbs = exphbs.create({ helpers });
// Sets up session and connect to our Sequelize db
// Configure and link a session object with the sequelize store
const sess = {
secret: 'Super secret secret',
// Express session will use cookies by default, but we can specify options for those cookies by adding a cookies property to our session options.
cookie: {
// maxAge sets the maximum age for the cookie to be valid. Here, the cookie (and session) will expire after one hour. The time should be given in milliseconds.
maxAge: 300000,
// httpOnly tells express-session to only store session cookies when the protocol being used to connect to the server is HTTP.
httpOnly: true,
// secure tells express-session to only initialize session cookies when the protocol being used is HTTPS. Having this set to true, and running a server without encryption will result in the cookies not showing up in your developer console.
secure: false,
// sameSite tells express-session to only initialize session cookies when the referrer provided by the client matches the domain out server is hosted from.
sameSite: 'strict',
},
resave: false,
saveUninitialized: true,
// Sets up session store
store: new SequelizeStore({
db: sequelize,
}),
};
// Add express-session and store as Express.js middleware
app.use(session(sess));
// Inform Express.js on which template engine to use
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// Middleware for parsing JSON and urlencoded form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Static middleware pointing to the public folder
app.use(express.static(path.join(__dirname, 'public')));
// Servers the routes to the server
app.use(routes);
// Sync database
sequelize.sync({ force: false }).then(() => {
console.log('Database synced');
}).catch(err => {
console.error('Error syncing database:', err);
});
// Start server
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});