-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
56 lines (46 loc) · 1.68 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');//package to create session and set cookies
const routes = require('./controllers');
const exphbs = require('express-handlebars');
const SequelizeStore = require('connect-session-sequelize')(session.Store);
const sequelize = require('./config/connection');// import sequelize connection
const helpers = require('./utils/helper');
const hbs = exphbs.create({helpers});
const app = express();
const PORT = process.env.PORT || 3001;
// Set up sessions with cookies
const sess = {
secret: 'Super secret secret',
cookie: {
httpOnly: true,
secure: false,
sameSite: false,
maxAge: 24 * 60 * 60 * 1000, // expires after 1 day
},
resave: false,
saveUninitialized: false,
//when we create new session it will be stored in db
store: new SequelizeStore({
db: sequelize,
checkExpirationInterval: 15 * 60 * 1000,
expiration: 24 * 60 * 60 * 1000
}),
};
app.engine('handlebars',hbs.engine);
app.set('view engine','handlebars');
//initialize middleware and passing session var, each time user creates request to our application we will call this middleware
app.use(session(sess));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname,'public')))
app.use(routes);
// sync sequelize models to the database, then turn on the server
// turn activate the connection to the DB and local host port server//
sequelize.sync({ force: false }).then(() => {
app.listen(PORT, () =>
console.log(
`\nServer running on port ${PORT}. Visit http://localhost:${PORT} and create an account!`
)
);
});