Good Code
The good version exports an app factory, so tests and workers can create the same app without starting a server.
Lesson 01
Create the Express app separately from the process that starts listening.
import express from "express";
import { createReviewsRouter } from "./reviews/router";
export function createApp() {
const app = express();
app.use(express.json());
app.use("/reviews", createReviewsRouter());
return app;
}import express from "express";
import { createReviewsRouter } from "./reviews/router";
const app = express();
app.use(express.json());
app.use("/reviews", createReviewsRouter());
app.listen(process.env.PORT || 3000);
export default app;The good version exports an app factory, so tests and workers can create the same app without starting a server.
The bad version starts listening as a module side effect. Importing the app for tests or scripts now opens a port unexpectedly.