Node.jsDébutant 6 min·Updated: 2026-09-07
Build a RESTful API with Node.js and Express (Production Architecture)
Learn to build scalable, production-ready REST APIs using the classic Node.js Express framework.
Concepts & Theory
Express provides lightweight routing abstractions for HTTP verbs (GET, POST, PUT, DELETE) and JSON serialization.
Middlewares compose pipeline interceptors for authentication, request parsing, and rate limiting.
Using standard HTTP response status codes (200, 201, 400, 404, 500) ensures seamless API consumer integration.
Code Example
Run in PlaygroundJAVASCRIPTPROJET CARTHAGE
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware pour parser les corps JSON
app.use(express.json());
// Base de données simulée en mémoire
let exercices = [
{ id: 1, titre: "Boucles Python", langage: "python" },
{ id: 2, titre: "Promesses JS", langage: "javascript" }
];
// GET /api/exercices - Liste complète
app.get('/api/exercices', (req, res) => {
res.json({ success: true, count: exercices.length, data: exercices });
});
// POST /api/exercices - Création
app.post('/api/exercices', (req, res) => {
const { titre, langage } = req.body;
if (!titre || !langage) {
return res.status(400).json({ error: "Le titre et le langage sont obligatoires." });
}
const nouvelExercice = { id: Date.now(), titre, langage };
exercices.push(nouvelExercice);
res.status(201).json({ success: true, data: nouvelExercice });
});
app.listen(PORT, () => {
console.log(`Serveur API Carthage en écoute sur http://localhost:${PORT}`);
});Common Pitfalls & Mistakes
✗ Omitting express.json() middleware
Incorrect / Error
app.post("/api", (req, res) => {
console.log(req.body); // undefined
});Idiomatic / Correct
app.use(express.json());Without the body parser middleware, `req.body` remains undefined on incoming JSON POST requests.
Frequently Asked Questions
What is the difference between PUT and PATCH in a REST API?▾
PUT replaces the entire entity representation, whereas PATCH performs partial field modifications.
Practice in Carthage
Start Node.js CourseMaster Node.js with interactive exercises
Complete hands-on code challenges in your browser with real-time feedback and progressive hints.
