-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (47 loc) · 2.1 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
const path = require('path');
const express = require('express');
const session = require('express-session');
const exphbs = require('express-handlebars');
// Initializes Sequelize with session store
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const routes = require('./controllers');
const sequelize = require('./config/connection');
const helpers = require('./utils/helpers');
const app = express();
const PORT = process.env.PORT || 3001;
// Sets up session and connect to our Sequelize db
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: 60 * 60 * 1000,
// 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,
}),
};
app.use(session(sess));
const hbs = exphbs.create({ helpers });
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
sequelize.sync({ force: false }).then(() => {
app.listen(PORT, () =>
console.log(
`\nServer running on port ${PORT}. Visit http://localhost:${PORT} and create an account!`
)
);
});