/** * Express application entry point. * @description Production-ready server with a health check, centralized error * handling, and graceful shutdown. The app is exported so it can be imported by * tests; it only starts listening when run directly. */ import cors from 'cors'; import dotenv from 'dotenv'; import express, { type NextFunction, type Request, type Response } from 'express'; import path from 'node:path'; import router from './routes'; dotenv.config({ quiet: true }); const app = express(); const port = Number(process.env.PORT || 3000); // Views app.use(cors()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); // Health check endpoint. app.get('/health', function (req: Request, res: Response) { res.status(200).json({ status: 'ok' }); }); app.use('/', router); // Centralized error-handling middleware (must be registered last). app.use(function ( err: Error & { status?: number }, req: Request, res: Response, next: NextFunction ) { console.error(err.stack); res .status(err.status || 500) .json({ error: err.message || 'Internal Server Error' }); }); if (require.main === module) { // Bind to 0.0.0.0 so the server is reachable from outside a container. const server = app.listen(port, '0.0.0.0', function () { console.log('Express server started on port ' + port); }); const shutdown = function (signal: string) { console.log(signal + ' received, shutting down gracefully'); server.close(function () { console.log('Server closed'); process.exit(0); }); }; process.on('SIGINT', function () { shutdown('SIGINT'); }); process.on('SIGTERM', function () { shutdown('SIGTERM'); }); } export default app;