Node.js — The fundamentals

Guide 04 / 4

Resources and review

Consult the official references, diagnose errors and check what you have learned.

Expected outcome :

Justify your choices and find a piece of information in the documentation.

Reading
0%

01 · Exploring the official documentation

The links are available at the bottom of each guide.

Node.js · HTTP and files

Find createServer, IncomingMessage and ServerResponse in the HTTP manual. Look for readFile in the file manual then compare with how the public files are read.

Mission : retrouver ce qui termine une réponse et expliquer pourquoi le serveur appelle end.

Node.js · Modules and tests

Read how type: module affects .js files. Explore the test runner and the use of cleanups after a test.

Mission : expliquer pourquoi creerServeur ne commence pas lui-même à écouter un port.

npm · Scripts and reproducible install

Read the documentation of npm run and npm ci. Identify the differences between the dependency-free Node project and the future addition of compilers.

Mission : expliquer pourquoi on conserve package-lock.json après installation des outils.

02 · Troubleshooting and checking what you have learned

Isolate the process, the address, the format or the contract before changing the code.

EADDRINUSE, missing module or refused import

EADDRINUSE indicates a busy port: stop the other server or change PORT. For an import that cannot be found, check the path and extension. For an incorrect module mode, check type: module.

node --version
PORT=3001 npm start

HTML response instead of JSON

Check the /api/recettes address, the port and the server contacted. In Network, look at the status and Content-Type. The client checks reponse.ok before interpreting the JSON.

Page : /
Collection : /api/recettes
Fiche : /api/recettes/1

Data disappears or a filter does not match

A restart deliberately resets the in-memory storage. The title filter ignores case and edge spaces, but respects accents. Compare the request with the initial data.

Repartir de trois recettes pour les résultats attendus de l’atelier.

Self-assessment and extensions

Explain the HTTP cycle, separate the server from the client, create a valid recipe and justify 400, 404, 415 and 422. Make the tests pass after a change.

Je sais :
□ structurer des modules ES ;
□ lire URL et corps JSON ;
□ valider et produire une réponse ;
□ distinguer mémoire et persistance ;
□ tester l’API via HTTP.
Remember :

Next: organise the styles with SCSS, then check the client with TypeScript. SQL persistence, authentication, authorisations and traffic limits are extensions for a deployed application.