- TypeScript
- Logger
- JWT & Auth
- DTO validation
- Static file server
- Environment & configuration
- Graceful process shutdown
Production: npm i --omit=dev
Development: npm i
🛠 Build: npm run build
🌐 Start server: npm run start
or npm start
.
├── dist/ (auto generated, javascript/transpiled code)
│
├── logs/ (auto generated, log files)
│
├── public/ (shared via API server with path '/static')
│
└── src/ (source code - converts to dist/)
Application starts at ./src/server.ts
server.ts
creates and starts API server (express application) & initializes process exit helper (used for graceful shutdown).
ExpressApplication.ts
configures, creates & closes the http server & sets API routers.
A Router (similar to controller) routes the incoming request to desired handler (similar to method) or other routers.
setRouters.ts
handles cookie parsing and authentication, routing request to other routers or directly to handlers.
Middlewares:
logger.ts
binds a callback on response finish event and writes/logs request information.jwtResolve.ts
reads token from cookie and attaches the payload intores.locals.user
; This middleware must be called after cookie parser.authGuard.ts
checks ifres.locals.user
object is defined or not; Responses 401 (unauthorized) if user payload is not defined, passes the request to next middleware/handler if user payload is defined. Must be called afterjwtResolve.ts
.dtoValidator.ts
validates user inputs. Requires a DTO constructor, calls expressjson()
middleware, validates query parameters on GET method and request body on POST method (as JSON); Responses 400 (bad request) if DTO validation fails, attaches the DTO intores.locals.dto
& passes the request to next middleware/handler if DTO is valid.