Rework env selection

This commit is contained in:
SebClem 2024-07-12 16:44:04 +02:00
parent a22962902d
commit bd0fbe81c2
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
2 changed files with 15 additions and 21 deletions

View File

@ -1,8 +1,7 @@
import cookieParser from "cookie-parser"; import cookieParser from "cookie-parser";
import cors from "cors"; import cors from "cors";
import errorHandler from "errorhandler";
import express from "express"; import express from "express";
import createError from "http-errors";
import morgan from "morgan"; import morgan from "morgan";
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
@ -20,10 +19,11 @@ app.use(
); );
app.set("port", process.env.PORT || 3000); app.set("port", process.env.PORT || 3000);
if (process.env.ACCESS_LOG == "true") {
// app.use( app.use(
// morgan("dev", { stream: { write: (message) => logger.debug(message) } }) morgan("dev", { stream: { write: (message) => logger.debug(message) } })
// ); );
}
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: false })); app.use(express.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(cookieParser());
@ -36,15 +36,4 @@ app.use("/v2/api/", apiV2Router);
---------------------------------------------------------- ----------------------------------------------------------
*/ */
// error handler
if (app.get("env") == "development") {
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// only use in development
app.use(errorHandler());
}
export default app; export default app;

View File

@ -1,16 +1,21 @@
import errorHandler from "errorhandler";
import "./env.js"; import "./env.js";
import errorHandler from "errorhandler";
import figlet from "figlet";
import createError from "http-errors";
import kleur from "kleur";
import app from "./app.js"; import app from "./app.js";
import logger from "./config/winston.js"; import logger from "./config/winston.js";
import postInit from "./postInit.js"; import postInit from "./postInit.js";
import figlet from "figlet";
import kleur from "kleur";
/** /**
* Error Handler. Provides full stack * Error Handler. Provides full stack
*/ */
if (process.env.NODE_ENV === "development") { if (process.env.NODE_ENV === "development") {
app.use(errorHandler()); app.use(errorHandler());
app.use((req, res, next) => {
next(createError(404));
});
} }
/** /**
@ -22,7 +27,7 @@ const server = app.listen(app.get("port"), () => {
`App is running at ` + `App is running at ` +
kleur.green().bold(`http://localhost:${app.get("port")}`) + kleur.green().bold(`http://localhost:${app.get("port")}`) +
" in " + " in " +
kleur.green().bold(app.get("env") as string) + kleur.green().bold(process.env.NODE_ENV || "production") +
" mode" " mode"
); );
logger.info(kleur.red().bold("Press CTRL-C to stop")); logger.info(kleur.red().bold("Press CTRL-C to stop"));