Skip to content

Commit

Permalink
[#7] feat: add more logging for app routes
Browse files Browse the repository at this point in the history
  • Loading branch information
glemenneo committed Sep 18, 2024
1 parent 139d23c commit 648f9f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
28 changes: 10 additions & 18 deletions backend/user-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cors from 'cors'
import express, { Express, NextFunction, Request, Response } from 'express'
import helmet from 'helmet'
import { ResponseError } from './types/ResponseError'
import { logger } from './server'

const app: Express = express()

Expand All @@ -27,28 +27,20 @@ app.use((request: Request, response: Response, next: NextFunction) => {
next()
})

// Health Check Route
app.get('/', (_: Request, response: Response) => {
response
.json({
message: 'Hello World!',
})
.status(200)
response.status(200)
})

// Handle When No Route Match Is Found
app.use((next: NextFunction) => {
const error: ResponseError = new Error()
error.status = 404
next(error)
// Not Found Route
app.use((response: Response) => {
response.status(404)
})

app.use((error: ResponseError, _: Request, response: Response) => {
response.status(error.status ?? 500)
response.json({
error: {
message: error.message,
},
})
// Default Error Handler
app.use((error: Error, request: Request, response: Response) => {
logger.error(`[Controller] [${request.method} ${request.baseUrl + request.path}] ${error.message}`)
response.status(500)
})

export default app
3 changes: 0 additions & 3 deletions backend/user-service/src/types/ResponseError.ts

This file was deleted.

0 comments on commit 648f9f7

Please sign in to comment.