Node.js — The fundamentals

Guide 03 / 4

Node.js workshop · À table !

Build a local HTTP API with Node.js's native modules: filterable list, recipe by identifier and validated add; display its data in the browser and test the HTTP responses.

Expected outcome :

Reproduce the project, check its behaviours and succeed at an independent modification.

Reading
0%

01 · Building the HTTP mini-blog

Reproduce the complete files below in the working project.

Step 1 · Creating the folder structure

Create package.json, src/, public/ and test/, then each file at its path. The initial dataset is a function that produces fresh recipes each time a server is created.

src/recettes.js : données, filtre et validation
src/server.js : routage et HTTP
src/index.js : lancement
public/ : interface du navigateur
test/api.test.js : vérification du contrat
Remember :

The course folder holds teaching JSON; the application files are to be created in mini-blog-node/.

Step 2 · Starting with no external dependency

Run npm start and open the address shown. The server provides index.html, style.css, app.js and the API. The page loads the recipes with fetch.

npm start
http://127.0.0.1:3000/
Remember :

Expected: three complete cards with ingredients and preparation; the loading message becomes a total of three recipes.

Step 3 · Running two searches

Type soupe then submit. Then empty the field, choose 20 minutes and submit. Observe the requests in the browser's Network tab.

q=soupe → 1 recette
duree=20 → soupe et salade, 2 recettes
q=tarte&duree=20 → 0 recette
Remember :

The page reloads the data over HTTP; it does not merely filter cards that are already there.

Step 4 · Creating then finding a recipe

Create compote.json with the complete file then send it with curl. Follow the Location address received and reload the list.

POST /api/recettes → 201
GET /api/recettes/{id reçu} → 200
GET /api/recettes → total 4
Remember :

Stopping then restarting Node brings the total back to 3. This version uses the process's memory, not MySQL nor a persistent JSON file.

02 · Testing, fixing and extending

Compare the statuses as much as the content.

Step 5 · Running the contract tests

Run npm test. Read the creation test, then find the one that prevents a negative duration and the one that refuses access to the sources.

npm test
Remember :

Expected: test passes. An error makes the command exit with a non-zero code.

Exercise · Add a 15-minute limit

Add a 15 minutes option in public/index.html. Check that no server change is needed and that only the salad appears on the initial dataset.

<option value="15">15 minutes</option>
Remember :

Solution: insert the option in #duree-max. The contract already accepts any whole duration between 0 and 1440.

Exercise · Refuse an unknown category

Create a copy of compote.json with categorie: "Goûter" and send the request. Explain where the request is rejected.

Attendu : 422, aucune recette ajoutée.
Remember :

Solution: validerRecette checks the list Entrée, Plat, Dessert. Adding a category would imply a contract decision and suitable tests.

Exercise · Strengthen the format test

Add to the test a POST request with Content-Type: text/plain and another containing malformed JSON. Check the expected statuses.

Format text/plain → 415
Corps "{" avec application/json → 400
Remember :

Compare with the following solution after trying.

Solution · Two distinct HTTP failures

Insert these assertions in the test before it closes, with the appeler function already defined.

assert.equal((await appeler("/api/recettes", { method: "POST", headers: { "Content-Type": "text/plain" }, body: "test" })).status, 415);
assert.equal((await appeler("/api/recettes", { method: "POST", headers: { "Content-Type": "application/json" }, body: "{" })).status, 400);