2020-06-26 16:59:54 +02:00
|
|
|
let express = require("express");
|
|
|
|
let path = require("path");
|
|
|
|
let cookieParser = require("cookie-parser");
|
2020-08-17 11:33:54 +02:00
|
|
|
let session = require("express-session");
|
2020-06-26 16:59:54 +02:00
|
|
|
let logger = require("morgan");
|
2020-08-17 11:33:54 +02:00
|
|
|
let config = require("./config/config.json");
|
2020-06-26 16:59:54 +02:00
|
|
|
|
|
|
|
let indexRouter = require("./routes/index");
|
2020-08-17 16:54:43 +02:00
|
|
|
let registerRouter = require("./routes/register");
|
2020-08-17 11:33:54 +02:00
|
|
|
let loginRouter = require("./routes/login");
|
|
|
|
let logoutRouter = require("./routes/logout");
|
2020-08-16 12:48:43 +02:00
|
|
|
let commandRouter = require("./routes/command");
|
|
|
|
let ordersRouter = require("./routes/orders");
|
2020-08-16 13:26:58 +02:00
|
|
|
let sandwichesRouter = require("./routes/sandwiches");
|
2020-08-17 13:22:52 +02:00
|
|
|
let adminRouter = require("./routes/admin");
|
2020-06-26 16:59:54 +02:00
|
|
|
|
|
|
|
let app = express();
|
2020-08-17 11:33:54 +02:00
|
|
|
let sess = {
|
|
|
|
secret: config.secret,
|
|
|
|
cookie: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (app.get("env") === "production") {
|
|
|
|
app.set("trust proxy", 1);
|
|
|
|
sess.cookie.secure = true;
|
|
|
|
}
|
2020-06-26 16:59:54 +02:00
|
|
|
|
|
|
|
// view engine setup
|
|
|
|
app.set("views", path.join(__dirname, "views"));
|
|
|
|
app.set("view engine", "pug");
|
|
|
|
|
|
|
|
app.use(logger("dev"));
|
|
|
|
app.use(express.json());
|
|
|
|
app.use(express.urlencoded({ extended: false }));
|
|
|
|
app.use(cookieParser());
|
2020-08-17 11:33:54 +02:00
|
|
|
app.use(session(sess));
|
2020-06-26 16:59:54 +02:00
|
|
|
app.use(express.static(path.join(__dirname, "public")));
|
|
|
|
|
|
|
|
app.use("/", indexRouter);
|
2020-08-17 16:54:43 +02:00
|
|
|
app.use("/register", registerRouter);
|
2020-08-17 11:33:54 +02:00
|
|
|
app.use("/login", loginRouter);
|
|
|
|
app.use("/logout", logoutRouter);
|
2020-08-14 20:08:00 +02:00
|
|
|
app.use("/command", commandRouter);
|
2020-08-16 12:48:43 +02:00
|
|
|
app.use("/orders", ordersRouter);
|
2020-08-16 13:26:58 +02:00
|
|
|
app.use("/sandwiches", sandwichesRouter);
|
2020-08-17 13:22:52 +02:00
|
|
|
app.use("/admin", adminRouter);
|
2020-06-26 16:59:54 +02:00
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
app.use((req, res) => {
|
|
|
|
res.render("error", {message: "Page not found", "error": {}})
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handler
|
|
|
|
app.use((err, req, res) => {
|
|
|
|
// set locals, only providing error in development
|
|
|
|
res.locals.message = err.message;
|
|
|
|
res.locals.error = req.app.get("env") === "development" ? err : {};
|
|
|
|
|
|
|
|
// render the error page
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render("error");
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|