From b4a5d816c0cfda575bae0b8a5fb6a167cbbc1d72 Mon Sep 17 00:00:00 2001 From: Peace Date: Thu, 5 Jun 2025 00:55:07 +0900 Subject: [PATCH] middleware --- catDataMocking/src/app.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/catDataMocking/src/app.ts b/catDataMocking/src/app.ts index cdd4bb6..8736715 100644 --- a/catDataMocking/src/app.ts +++ b/catDataMocking/src/app.ts @@ -8,11 +8,35 @@ const port: number = 8000; app.use(cors()); +// 전체 적용 +app.use((req, res, next) => { + console.log(req.rawHeaders[1]); + next(); +}); + +// 특정 REST 엔드포인트 적용 +app.get("/cats/blue", (req, res) => { + console.log("This is /cats/blue GET middleware"); +}); + app.get("/", (req: express.Request, res: express.Response) => { - console.log(req); res.send({ cats: Cat }); }); +app.get("/cats/blue", (req, res) => { + res.send({ blue: Cat[0] }); +}); +app.get("/cats/som", (req, res) => { + res.send({ som: Cat[1] }); +}); + +// not found +app.use((req, res, next) => { + console.log(req.rawHeaders[1]); + console.log("This is 404 middleware"); + res.send({ error: "404 not found error" }); +}); + app.listen(port, () => { console.log(`server is on ${port}`); });