-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (68 loc) · 2.07 KB
/
app.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
const express = require("express");
const app = express();
const path = require("path");
const csurf = require("tiny-csrf");
var passport = require("passport");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const router = require("./routes");
const flash = require("connect-flash");
const Sequelize = require("sequelize");
const process = require("process");
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/config/config.json")[env];
var SequelizeStore = require("connect-session-sequelize")(session.Store);
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
}
var sessionStore = new SequelizeStore({
db: sequelize,
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser("any secret string"));
app.use(
session({
secret: "any secret string",
store: sessionStore,
cookie: {
maxAge: 24 * 60 * 60 * 1000, // 24 hours
},
})
);
// Run once to create session store table
sessionStore.sync();
// Middleware to remove the trailing "/" from a route
app.use((request, response, next) => {
if (request.path.slice(-1) == "/" && request.path.length > 1) {
const query = request.url.slice(request.path.length);
response.redirect(301, request.path.slice(0, -1) + query);
} else {
next();
}
});
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
app.use(csurf("this_should_be_32_character_long", ["POST", "PUT", "DELETE"]));
// Connect Flash Implementation
app.use(flash());
app.use(function (request, response, next) {
response.locals.messages = request.flash();
next();
});
// Import PassportJS config
require("./config/passport");
app.use(passport.initialize());
app.use(passport.session());
/* Import all routes from ./routes/index.js */
app.use(router);
module.exports = app;