MangoRecipe/Source/Webserver/index.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-10-17 11:56:45 +02:00
import express from "express";
2022-10-18 12:30:12 +02:00
import { Liquid } from "liquidjs";
2022-10-19 12:21:58 +02:00
import path from "path";
//import { apiRoutes } from "./routes/api";
//import { authRoutes } from "./routes/auth";
import { indexRoutes } from "./routes/index";
const isProduction = process.env.NODE_ENV === "production";
const LAYOUTS_DIRECTORY = path.join(__dirname, "layouts");
const PARTIALS_DIRECTORY = path.join(__dirname, "partials");
const PUBLIC_DIRECTORY = path.join(__dirname, "public");
const VIEWS_DIRECTORY = path.join(__dirname, "views");
2022-10-19 13:07:30 +02:00
export function startWebserver(config: any) {
2022-10-19 12:21:58 +02:00
const app = express();
app.disable("x-powered-by");
app.use(express.urlencoded({ extended: true, limit: "1mb" }));
app.use(express.json({ limit: "1mb" }));
// Liquid engine options
const engine = new Liquid({
root: [VIEWS_DIRECTORY, PARTIALS_DIRECTORY, LAYOUTS_DIRECTORY],
cache: isProduction,
lenientIf: true,
jsTruthy: true,
extname: ".liquid",
});
// View engine options
app.engine("liquid", engine.express());
app.set("views", [VIEWS_DIRECTORY, PARTIALS_DIRECTORY, LAYOUTS_DIRECTORY]);
app.set("view engine", "liquid");
// Uses public folder
app.use("/public/", express.static(PUBLIC_DIRECTORY));
// Routes
app.use("/", indexRoutes());
2022-10-19 13:07:30 +02:00
2022-10-19 12:21:58 +02:00
// 404 handler
app.use((req, res) => {
if (req.accepts("html")) return res.status(404).render("404", { url: req.url, errorCode: 404 });
else if (req.accepts("json")) return res.status(404).send({ error: "404" });
else res.status(404).type("txt").send("404");
});
app.listen(3430, "0.0.0.0", () => {
console.log(`Webserver running on port 3430`);
});
2022-10-19 13:07:30 +02:00
}