Good Code
The good version keeps startup in one entry point and lets the app wiring be imported without immediately listening on a port.
Lesson 01
Keep process startup separate from reusable application wiring.
import { createApp } from "./app";
import { readConfig } from "./config";
const config = readConfig(process.env);
const app = createApp({ logger: console });
const server = app.listen(config.port, () => {
console.info("Server listening on port " + config.port);
});
export { server };import http from "node:http";
import { handleRequest } from "./handler";
const server = http.createServer(handleRequest);
server.listen(process.env.PORT || 3000);
export default server;The good version keeps startup in one entry point and lets the app wiring be imported without immediately listening on a port.
The bad version starts the server as a side effect of importing app.ts, which makes tests, scripts, and workers unexpectedly open sockets.