1const express = require("express");
2const path = require("path");
3const cookieParser = require("cookie-parser");
4const logger = require("morgan");
5const flash = require("connect-flash");
6
7const helpers = require("./helpers");
8const handlers = require("./middlewares/handlers.middleware");
9const router = require("./routes/index");
10const session = require("./session");
11
12const app = express();
13
14// setting session
15app.use(session);
16
17app.use(flash());
18// thanks to wesbos
19// pass variables to our templates + all requests
20app.use((req, res, next) => {
21 res.locals.h = helpers;
22 res.locals.flashes = req.flash();
23 res.locals.user = req.session.user || null;
24 res.locals.currentPath = req.path;
25 res.locals.fullUrl = `${req.protocol}://${req.get("host")}${req.originalUrl}`;
26 res.locals.fullHost = `${req.protocol}://${req.get("host")}`;
27 next();
28});
29
30// view engine setup
31app.set("views", path.join(__dirname, "views"));
32app.set("view engine", "pug");
33
34app.use(logger("dev"));
35app.use(express.json());
36app.use(express.urlencoded({ extended: false }));
37app.use(cookieParser());
38app.use(express.static(path.join(__dirname, "public")));
39
40app.use("/", router);
41
42// error handler
43app.use(handlers.notFound);
44app.use(handlers.errorHandler);
45
46module.exports = app;