From ae222a20774c43174713f4acbfb5084786a4cc7f Mon Sep 17 00:00:00 2001 From: billisdead Date: Sat, 16 May 2026 12:27:25 +0200 Subject: [PATCH] api server corrections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - index: fix app.listen() error handling — callback never receives err, bind failures are emitted as 'error' events; use server.on('error', ...) - app: add error-handling middleware to catch unhandled route errors and return a safe 500 JSON response instead of leaking stack traces - package.json: remove unused cookie-parser and @types/cookie-parser Co-Authored-By: Claude Sonnet 4.6 --- artifacts/api-server/package.json | 2 -- artifacts/api-server/src/app.ts | 7 ++++++- artifacts/api-server/src/index.ts | 12 ++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/artifacts/api-server/package.json b/artifacts/api-server/package.json index 1c15a36..dcafba0 100644 --- a/artifacts/api-server/package.json +++ b/artifacts/api-server/package.json @@ -12,7 +12,6 @@ "dependencies": { "@workspace/api-zod": "workspace:*", "@workspace/db": "workspace:*", - "cookie-parser": "^1.4.7", "cors": "^2", "drizzle-orm": "catalog:", "express": "^5", @@ -20,7 +19,6 @@ "pino-http": "^10" }, "devDependencies": { - "@types/cookie-parser": "^1.4.10", "@types/cors": "^2.8.19", "@types/express": "^5.0.6", "@types/node": "catalog:", diff --git a/artifacts/api-server/src/app.ts b/artifacts/api-server/src/app.ts index f32f71e..c5d1cd2 100644 --- a/artifacts/api-server/src/app.ts +++ b/artifacts/api-server/src/app.ts @@ -1,4 +1,4 @@ -import express, { type Express } from "express"; +import express, { type Express, type NextFunction, type Request, type Response } from "express"; import cors from "cors"; import pinoHttp from "pino-http"; import router from "./routes"; @@ -31,4 +31,9 @@ app.use(express.urlencoded({ extended: true })); app.use("/api", router); +app.use((err: Error, _req: Request, res: Response, _next: NextFunction) => { + logger.error({ err }, "Unhandled error"); + res.status(500).json({ error: "Internal server error" }); +}); + export default app; diff --git a/artifacts/api-server/src/index.ts b/artifacts/api-server/src/index.ts index b1f024d..aed4c50 100644 --- a/artifacts/api-server/src/index.ts +++ b/artifacts/api-server/src/index.ts @@ -15,11 +15,11 @@ if (Number.isNaN(port) || port <= 0) { throw new Error(`Invalid PORT value: "${rawPort}"`); } -app.listen(port, (err) => { - if (err) { - logger.error({ err }, "Error listening on port"); - process.exit(1); - } - +const server = app.listen(port, () => { logger.info({ port }, "Server listening"); }); + +server.on("error", (err) => { + logger.error({ err }, "Error listening on port"); + process.exit(1); +});