Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Node.js Core & Express Backend APIs 18 mins read Free Lesson

Building RESTful APIs with Express.js Framework

Scaffold an Express web server, handle GET, POST, PUT, DELETE routes, and parse JSON payloads.

Express.js REST API Scaffolding

const express = require('express');
const app = express();

app.use(express.json()); // Built-in JSON body parser

const courses = [
    { id: 1, title: 'Node.js Masterclass', is_free: true }
];

app.get('/api/courses', (req, res) => {
    res.status(200).json(courses);
});

app.post('/api/courses', (req, res) => {
    const newCourse = { id: courses.length + 1, ...req.body };
    courses.push(newCourse);
    res.status(201).json(newCourse);
});

app.listen(3000, () => console.log('Server running on port 3000'));