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 cors from "cors";
import errorHandler from "errorhandler";
import express from "express";
import createError from "http-errors";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";
@ -20,10 +19,11 @@ app.use(
);
app.set("port", process.env.PORT || 3000);
// app.use(
// morgan("dev", { stream: { write: (message) => logger.debug(message) } })
// );
if (process.env.ACCESS_LOG == "true") {
app.use(
morgan("dev", { stream: { write: (message) => logger.debug(message) } })
);
}
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
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;

View File

@ -1,16 +1,21 @@
import errorHandler from "errorhandler";
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 logger from "./config/winston.js";
import postInit from "./postInit.js";
import figlet from "figlet";
import kleur from "kleur";
/**
* Error Handler. Provides full stack
*/
if (process.env.NODE_ENV === "development") {
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 ` +
kleur.green().bold(`http://localhost:${app.get("port")}`) +
" in " +
kleur.green().bold(app.get("env") as string) +
kleur.green().bold(process.env.NODE_ENV || "production") +
" mode"
);
logger.info(kleur.red().bold("Press CTRL-C to stop"));