forked from Aaronf87/UnityBiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (53 loc) · 1.93 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
// Import node.js path module provides utilities for working with file and directory paths.
const path = require("path");
// Imports Express.js.
const express = require("express");
// Import express-session
const session = require("express-session");
const SequelizeStore = require("connect-session-sequelize")(session.Store);
// Import dotenv environment variables.
require("dotenv").config();
// Import the custom helper methods
const helpers = require("./util/helpers");
// Import express-handlebars
const exphbs = require("express-handlebars");
const hbs = exphbs.create({ helpers });
// Import the routes.
const routes = require("./controllers");
// Import the connection object: Sequelize connection.
const sequelize = require("./config/connection");
// Sets up the Express App
const app = express();
const PORT = process.env.PORT || 3001;
// Set up sessions with cookies
const sess = {
secret: process.env.SESSION_PASSWORD,
cookie: {
maxAge: 1 * 60 * 60 * 1000, // 1 hour
httpOnly: true,
secure: false,
sameSite: "strict",
},
resave: false,
saveUninitialized: true,
// Sets up session store where we will hold the cookie
store: new SequelizeStore({
db: sequelize,
}),
};
// Set Handlebars.js as the default template engine.
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");
// Middleware to handle session.
app.use(session(sess));
// Middleware for parsing JSON and urlencoded form data.
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Middleware pointing to the public directory (absolute path).
app.use(express.static(path.join(__dirname, "public")));
//Send all the requests that begin with / to the index.js in the routes folder.
app.use(routes);
// Synchronize sequelize models to the database before starting Express.js server, then turn on the server
sequelize.sync({ force: false }).then(() => {
app.listen(PORT, () => console.log("Now listening!"));
});