First server
From a single file to a structure you won't regret in six months.
Your First Express Server
Almost every Express project starts with a forty-line index.js file, and almost all of them end up with a two-thousand-line index.js where the database connection, input validation, business rules, and server configuration all live together. The shift never happens through a conscious decision: each individual addition seems reasonable, and one day no one dares touch the file because everything in it is coupled to everything else.
The problem isn't aesthetic. A server that listens in the same module that defines the routes is a server you can't test without opening a port. A configuration read with process.env in the middle of business code is a configuration you discover is missing only in production, on the first request, three hours after deployment. This lesson therefore builds a minimal server while putting in place right away the three separations that cost five minutes now and save you weeks later: configuration separated from code, application separated from server, routes separated from logic.
Installation
mkdir mon-api && cd mon-api
npm init -y
npm install express
npm install --save-dev nodemon
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion