N06

Middleware Stack Express

Express morgan ยท cors

Un serveur Express dรฉmontrant une stack middleware complรจte : parsing JSON, logging avec morgan, CORS, timer de rรฉponse custom et authentification par token. Illustre l'ordre d'exรฉcution et la chaรฎne next().

โ–ถ Lancer le projet

# 1. Aller dans le dossier
cd 06-middleware/mini-projet

# 2. Installer les dรฉpendances (express, morgan, cors)
npm install

# 3. Dรฉmarrer le serveur
npm start          # node server.js

# Le serveur dรฉmarre sur http://localhost:3006

Stack Middleware โ€” ordre d'exรฉcution

  Requรชte HTTP entrante
         โ”‚
         โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  express.json()     โ”‚  โ† parse le body JSON
  โ”‚  express.urlencoded โ”‚  โ† parse les formulaires
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  morgan('dev')      โ”‚  โ† log la requรชte HTTP
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  cors()             โ”‚  โ† ajoute les headers CORS
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  timer (custom)     โ”‚  โ† mesure la durรฉe de rรฉponse
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  auth (custom)      โ”‚  โ† vรฉrifie Authorization header
  โ”‚  (skip /public)     โ”‚    sauf pour les routes publiques
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Routes             โ”‚  โ† /health /public /private /echo
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  404 handler        โ”‚  โ† route non trouvรฉe
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚
            โ–ผ
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Error handler      โ”‚  โ† erreurs serveur (4 params)
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Routes disponibles

Mรฉthode URL Auth Description
GET /health Public Statut serveur + uptime
GET /public Public Route sans authentification
GET /private Token requis Route protรฉgรฉe par le middleware auth
POST /echo Token requis Renvoie le body JSON reรงu

Tester avec curl

# Route publique โ€” pas de token
curl http://localhost:3006/health
curl http://localhost:3006/public

# Route protรฉgรฉe โ€” sans token โ†’ 401
curl http://localhost:3006/private

# Route protรฉgรฉe โ€” avec token
curl http://localhost:3006/private \
  -H "Authorization: Bearer montoken123"

# POST echo avec body JSON
curl -X POST http://localhost:3006/echo \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer montoken123" \
  -d '{"message":"Hello middleware !","test":true}'

# Route inexistante โ†’ 404
curl http://localhost:3006/inconnu

Code source โ€” server.js

/* Chargement... */
โš ๏ธ Ce projet tourne localement โ€” Node.js et npm doivent รชtre installรฉs. Lancez npm install pour installer express, morgan et cors. Tรฉlรฉchargez Node.js sur nodejs.org.
โ† Cours N06 N07 โ€” Routing โ†’