From d0ebb10ef292153fa3a84a5f89bff4c7a8fd04fb Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 10 Jun 2024 18:54:48 -0700 Subject: [PATCH 1/4] refactor catch all route handler - move connectDB fn into startServer fn --- server/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/index.ts b/server/index.ts index 92cbfac..f93f887 100644 --- a/server/index.ts +++ b/server/index.ts @@ -11,8 +11,8 @@ import devRoutes from './routes/devRoutes'; import connectDB from './config/db'; import dotenv from 'dotenv'; import cookieParser from 'cookie-parser'; -import { notFound } from './controllers/errorControllers'; import errorHandler from './middleware/errorHandler'; +import { NotFoundError } from './errors'; dotenv.config(); @@ -21,8 +21,6 @@ const app: Application = express(); app.use(express.json()); app.use(cookieParser()); -connectDB(); - app.get('/health', (req: Request, res: Response) => { res.status(200).send('OK'); }); @@ -49,13 +47,17 @@ if (process.env.NODE_ENV === 'production') { }); } -app.use(notFound); +app.use((_req, _res) => { + throw new NotFoundError(); +}); app.use(errorHandler); const PORT: number = Number(process.env.PORT) || 3000; export const startServer = () => { + connectDB(); + return app.listen(PORT, () => console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`), ); From 2a42855bf44a4ccf94723d668cb3894630a2d826 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 10 Jun 2024 18:55:04 -0700 Subject: [PATCH 2/4] refactor not found handler test --- __tests__/errorController.test.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/__tests__/errorController.test.ts b/__tests__/errorController.test.ts index 04e5b39..a566d5b 100644 --- a/__tests__/errorController.test.ts +++ b/__tests__/errorController.test.ts @@ -1,7 +1,8 @@ +import app from '../server/index'; +import request from 'supertest'; import { Request, Response, NextFunction } from 'express'; -import { notFound } from '../server/controllers/errorControllers'; import errorHandler from '../server/middleware/errorHandler'; -import { BadRequestError } from '../server/errors'; +import { BadRequestError, NotFoundError } from '../server/errors'; describe('Middleware Tests', () => { let mockRequest: Partial; @@ -18,10 +19,13 @@ describe('Middleware Tests', () => { }); describe('notFound Middleware', () => { - it('should return 404 and the original URL', () => { - notFound(mockRequest as Request, mockResponse as Response, mockNext); - expect(mockResponse.status).toHaveBeenCalledWith(404); - expect(mockNext).toHaveBeenCalled(); + it('should return 404 and the original URL', async () => { + const exampleNotFoundError = new NotFoundError(); + + const response = await request(app).get('/non-existent-route').send(); + + expect(response.status).toEqual(404); + expect(response.body).toEqual(exampleNotFoundError.serializeErrors()); }); }); From 22551a3994983c14cfdd4e9ee708a529741832a5 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 10 Jun 2024 18:55:53 -0700 Subject: [PATCH 3/4] remove unused catch all controller --- server/controllers/errorControllers.ts | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 server/controllers/errorControllers.ts diff --git a/server/controllers/errorControllers.ts b/server/controllers/errorControllers.ts deleted file mode 100644 index 4c7b2ec..0000000 --- a/server/controllers/errorControllers.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Request, Response, NextFunction } from 'express'; - -const notFound = (req: Request, res: Response, next: NextFunction): void => { - const error = new Error(`Not found - ${req.originalUrl}`); - res.status(404); - next(error); -}; - -export { notFound }; From 16d2fbcc7b8ea68dd9e66491d8d80c13a72a9ad1 Mon Sep 17 00:00:00 2001 From: seantokuzo Date: Mon, 10 Jun 2024 19:28:13 -0700 Subject: [PATCH 4/4] linty mcLinterson --- server/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/index.ts b/server/index.ts index f93f887..275a568 100644 --- a/server/index.ts +++ b/server/index.ts @@ -57,7 +57,7 @@ const PORT: number = Number(process.env.PORT) || 3000; export const startServer = () => { connectDB(); - + return app.listen(PORT, () => console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`), );