From bd0fbe81c27a12c0a8cf479ab1843fe6e3f3d438 Mon Sep 17 00:00:00 2001 From: SebClem Date: Fri, 12 Jul 2024 16:44:04 +0200 Subject: [PATCH] Rework env selection --- nextcloud_backup/backend/src/app.ts | 23 ++++++----------------- nextcloud_backup/backend/src/server.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/nextcloud_backup/backend/src/app.ts b/nextcloud_backup/backend/src/app.ts index a68c876..788ff06 100644 --- a/nextcloud_backup/backend/src/app.ts +++ b/nextcloud_backup/backend/src/app.ts @@ -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; diff --git a/nextcloud_backup/backend/src/server.ts b/nextcloud_backup/backend/src/server.ts index 5228a13..599169d 100755 --- a/nextcloud_backup/backend/src/server.ts +++ b/nextcloud_backup/backend/src/server.ts @@ -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"));