-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (62 loc) · 1.78 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
69
70
71
const logger = require("pino")();
const api = require("./api");
const auth = require("./auth");
const express = require("express");
const passport = require("passport");
const cookieParser = require("cookie-parser");
const db = require("./db");
const cors = require("cors")
const app = express();
app.set("trust proxy", true);
app.use(cors({origin:true, credentials: true}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
if (process.env.NODE_ENV == 'production'){
app.use(express.static('client/build'));
}
// cookie parser middleware
app.use(cookieParser());
const session = require("express-session");
const MongoStore = require("connect-mongo");
const oneDay = 1000 * 60 * 60 * 24;
app.use(
session({
secret: process.env.SESSION_SECRET,
store: MongoStore.create({
clientPromise: db.init(),
dbName: process.env.MONGO_DATABASE
}),
cookie: { maxAge: oneDay },
resave: false,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
// Redirect to non-www url
app.get("*", (req, res, next) => {
if (req.headers.host.slice(0, 4) === "www.") {
const newHost = req.headers.host.slice(4);
return res.redirect(301, req.protocol + "://" + newHost + req.originalUrl);
}
next();
});
app.use("/api", api);
app.use("/auth", auth);
// any server errors cause this function to run
app.use((err, req, res, next) => {
const status = err.status || 500;
if (status === 500) {
logger.error("The server errored when processing a request!");
logger.error(err);
}
res.status(status);
res.send({
status: status,
message: err.message,
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
logger.info(`Server running on port: ${port}`);
});