-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (51 loc) · 1.82 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
"use strict";
const { join } = require("path");
const express = require("express");
const createError = require("http-errors");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const serveFavicon = require("serve-favicon");
const expressSession = require("express-session");
const MongoStore = require("connect-mongo")(expressSession);
const mongoose = require("mongoose");
const deserializeUserMiddleware = require("./middleware/deserialize-user");
const azulejoRouter = require("./routes/azulejo");
const authRouter = require("./routes/auth");
const app = express();
app.use(serveFavicon(join(__dirname, "public/images", "favicon.ico")));
app.use(express.static(join(__dirname, "client/build")));
app.use(logger("dev"));
app.use(express.urlencoded({ extended: false, limit: "2mb" }));
app.use(express.json({ limit: "2mb" }));
app.use(cookieParser());
app.use(
expressSession({
secret: process.env.SESSION_SECRET,
cookie: { maxAge: 60 * 60 * 24 * 1000 },
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: mongoose.connection,
ttl: 24 * 60 * 60
})
})
);
app.use(deserializeUserMiddleware);
app.use("/api/", azulejoRouter);
app.use("/api/auth", authRouter);
app.get("*", (req, res, next) => {
res.sendFile(join(__dirname, "./client/build/index.html"));
});
// Catch missing routes and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// Catch all error handler
app.use((error, req, res, next) => {
// Set error information, with stack only available in development
res.locals.message = error.message;
res.locals.error = req.app.get("env") === "development" ? error : {};
res.status(error.status || 500);
res.json({ type: "error", error: { message: error.message } });
});
module.exports = app;