Create the Express Application
Create src/app.js.
This file will initialize the Express application and register global middleware and routes.
We will keep app initialization separate from server startup. This makes the application easier to test using Supertest without opening a network port.
src/app.js
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
const app = express();
app.use(helmet());
app.use(cors({
origin: process.env.CLIENT_ORIGIN || 'http://localhost:5173'
}));
app.use(express.json({ limit: '100kb' }));
app.use(express.urlencoded({
extended: true,
limit: '100kb'
}));
app.get('/health', (req, res) => {
res.status(200).json({
success: true,
message: 'API is running'
});
});
export default app;Creates an Express application, registers security headers, CORS, body parsing middleware, and a health endpoint.
Key Takeaways
Express applications are initialized using express().
Middleware processes requests before route handlers.
Separate application initialization from server startup.