Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/app.js

97 lines
3 KiB
JavaScript
Raw Normal View History

2020-06-26 16:59:54 +02:00
let express = require("express");
let path = require("path");
let cookieParser = require("cookie-parser");
let session = require("express-session");
2020-06-26 16:59:54 +02:00
let logger = require("morgan");
2020-08-18 16:33:05 +02:00
let { I18n } = require("i18n");
2020-08-20 22:46:46 +02:00
let Recaptcha = require("express-recaptcha").RecaptchaV3;
2020-08-21 12:41:34 +02:00
let SMTPClient = require("emailjs").SMTPClient;
2020-08-20 22:46:46 +02:00
let config = process.env.NODE_ENV === "test" ? {} : require("./config/config.json");
2020-06-26 16:59:54 +02:00
let indexRouter = require("./routes/index");
let registerRouter = require("./routes/register");
let loginRouter = require("./routes/login");
2020-08-25 16:46:02 +02:00
let forgetRouter = require("./routes/forget");
let logoutRouter = require("./routes/logout");
2020-08-18 17:54:00 +02:00
let orderRouter = require("./routes/order");
let ordersRouter = require("./routes/orders");
2020-08-16 13:26:58 +02:00
let sandwichesRouter = require("./routes/sandwiches");
2020-08-17 17:58:55 +02:00
let profileRouter = require("./routes/profile");
2020-08-25 15:40:06 +02:00
let checkRouter = require("./routes/check");
2020-08-17 13:22:52 +02:00
let adminRouter = require("./routes/admin");
2020-08-21 12:41:34 +02:00
let contactRouter = require("./routes/contact");
2020-06-26 16:59:54 +02:00
let app = express();
let sess = {
2020-08-20 22:46:46 +02:00
secret: process.env.NODE_ENV === "test" ? "Keyboard Cat" : config.secret,
cookie: {}
2020-08-18 15:49:58 +02:00
};
2020-08-18 16:33:05 +02:00
let i18n = new I18n({
locales: ["fr", "en"],
defaultLocale: "fr",
cookie: "locale",
directory: __dirname + "/locales",
objectNotation: true
});
2020-08-21 12:41:34 +02:00
let recaptcha = process.env.NODE_ENV === "test" ? null : new Recaptcha(config.reCaptcha.siteKey,
config.reCaptcha.secretKey, {callback: "cb"});
let mailClient = new SMTPClient(process.env.NODE_ENV === "test" ? {} : config.email.server);
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");
2020-08-21 12:41:34 +02:00
app.set("config", config);
2020-08-20 22:46:46 +02:00
app.set("recaptcha", recaptcha);
2020-08-21 12:41:34 +02:00
app.set("mailClient", mailClient);
2020-06-26 16:59:54 +02:00
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
2020-06-26 16:59:54 +02:00
app.use(cookieParser());
app.use(session(sess));
2020-06-26 16:59:54 +02:00
app.use(express.static(path.join(__dirname, "public")));
2020-08-18 16:33:05 +02:00
app.use(i18n.init);
if(process.env.NODE_ENV === "test")
app.locals.test = true;
app.use((req, res, next) => {
res.locals.user = req.session.user;
2020-08-20 22:46:46 +02:00
res.locals.captcha = process.env.NODE_ENV === "test" ? undefined : recaptcha.render();
next();
});
2020-06-26 16:59:54 +02:00
app.use("/", indexRouter);
app.use("/register", registerRouter);
app.use("/login", loginRouter);
2020-08-25 16:46:02 +02:00
app.use("/forget", forgetRouter);
app.use("/logout", logoutRouter);
2020-08-18 17:54:00 +02:00
app.use("/order", orderRouter);
app.use("/orders", ordersRouter);
2020-08-16 13:26:58 +02:00
app.use("/sandwiches", sandwichesRouter);
2020-08-17 17:58:55 +02:00
app.use("/profile", profileRouter);
2020-08-25 15:40:06 +02:00
app.use("/check", checkRouter);
2020-08-17 13:22:52 +02:00
app.use("/admin", adminRouter);
2020-08-21 12:41:34 +02:00
app.use("/contact", contactRouter);
2020-06-26 16:59:54 +02:00
// catch 404 and forward to error handler
app.use((req, res) => {
res.status(404);
res.render("error", {title: "SOD - Page not found", message: "Page not found", "error": {}})
2020-06-26 16:59:54 +02:00
});
// error handler
app.use((err, req, res) => {
// render the error page
res.status(err.status || 500);
2020-08-20 12:14:38 +02:00
res.render("error", {
message: err.message,
error: req.app.get("env") === "development" ? err : {}
});
2020-06-26 16:59:54 +02:00
});
module.exports = app;