-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
70 lines (59 loc) · 2.02 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
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const logger = require("morgan");
const Sequelize = require("sequelize");
const session = require("express-session");
const SequelizeStore = require("connect-session-sequelize")(session.Store);
const db = require("./models");
const app = express();
const ALLOWED_ORIGINS = process.env.ALLOWED_ORIGINS && JSON.parse(process.env.ALLOWED_ORIGINS) || [];
app.use(function (req, res, next) {
const origin = req.headers.origin;
const theOrigin = ALLOWED_ORIGINS.indexOf(origin) >= 0 ? origin : ALLOWED_ORIGINS[0];
res.header("Access-Control-Allow-Origin", theOrigin);
res.header("Access-Control-Allow-Credentials", "true");
res.header(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.use(logger("dev"));
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.set("trust proxy", 1); // trust first proxy
db.sequelize.define("Session", {
sid: {
type: Sequelize.STRING,
primaryKey: true,
},
rememberme: Sequelize.BOOLEAN,
expires: Sequelize.DATE,
data: Sequelize.TEXT,
});
app.use(
session({
name: "app.sid",
secret: process.env.DOMAIN,
store: new SequelizeStore({
db: db.sequelize,
checkExpirationInterval: 15 * 60 * 1000, // The interval at which to cleanup expired sessions in milliseconds.
expiration: 15 * 24 * 60 * 60 * 1000, // The maximum age (in milliseconds) of a valid session.
}),
resave: true,
saveUninitialized: true,
})
);
app.use("/api", require("./routes/index"));
app.use("/notification", require("./routes/pushNotification"));
app.use(require("./handlers/error-handler"));
module.exports = app;