REST APIs & Middleware in Node.js

8 min read JAVASCRIPT 6 views

Creating Robust RESTful Endpoints

REST (Representational State Transfer) is the architectural pattern behind modern web services. Learn how HTTP verbs (GET, POST, PUT, DELETE) map to CRUD operations, and how Express middleware chains inspect and transform requests.

javascript Example
Run Code
const express = require('express');
const app = express();

// Custom logging middleware
app.use((req, res, next) => {
    console.log('[' + new Date().toISOString() + '] ' + req.method + ' ' + req.url);
    next();
});

const courses = [{ id: 1, name: 'Node.js' }, { id: 2, name: 'React' }];
app.get('/api/courses', (req, res) => res.json(courses));
app.get('/api/courses/:id', (req, res) => {
    const course = courses.find(c => c.id === parseInt(req.params.id));
    if (!course) return res.status(404).json({ error: 'Course not found' });
    res.json(course);
});

Related Tutorials