From fcf174376d4ae44a236595cf3b284df1ce309bb6 Mon Sep 17 00:00:00 2001 From: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Date: Thu, 18 Jan 2024 18:45:01 +0000 Subject: [PATCH] Clean up the request correctly --- packages/controller/package.json | 2 ++ packages/controller/src/Controller.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/controller/package.json b/packages/controller/package.json index 042a53ad..4af76e17 100644 --- a/packages/controller/package.json +++ b/packages/controller/package.json @@ -24,6 +24,7 @@ "busboy": "^1.6.0", "compression": "^1.7.4", "express": "^4.18.2", + "finalhandler": "^1.2.0", "fs-extra": "^11.1.1", "jsonwebtoken": "^9.0.1", "jszip": "^3.10.1", @@ -39,6 +40,7 @@ "@types/busboy": "^1.5.0", "@types/compression": "^1.7.2", "@types/express": "^4.17.17", + "@types/finalhandler": "^1.2.0", "@types/fs-extra": "^11.0.1", "@types/jsonwebtoken": "^9.0.2", "@types/node": "^20.4.5", diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 42c93d2d..f89753c3 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -3,6 +3,7 @@ import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net"; import type { ControllerArgs } from "../controller"; import express, { type Request, type Response, type NextFunction, type Application } from "express"; import type { Static } from "@sinclair/typebox"; +import finalhandler from "finalhandler"; import compression from "compression"; import events, { EventEmitter } from "events"; @@ -173,7 +174,7 @@ export default class Controller { const durationMs = (Number(endNs - startNs) / 1e6); logger.log({ level: "http", - message: `HTTP ${req.httpVersion} ${req.method} ${req.originalUrl}` + + message: `HTTP${req.httpVersion} ${req.method} ${req.originalUrl}` + ` (Status ${res.statusCode} in ${durationMs}ms)`, meta: { method: req.method, @@ -324,7 +325,9 @@ export default class Controller { }, }); - next(); + // Create and call the final handler, this is the same method used by express + // This is used instead of next(err) to stop express from logging the error to the console + finalhandler(req, res, { env: this.app.get("env") })(err); }); this.wsServer = new WsServer(this);