Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials Node.js REST API Design
Node.js Intermediate FREE

REST API Design

Lesson 1 of 17 Intermediate Interactive

REST APIs use HTTP methods (GET, POST, PUT, DELETE) to perform CRUD operations.

Syntax

NODEJS
GET    /api/users       → List users
GET    /api/users/:id    → Get user
POST   /api/users       → Create user
PUT    /api/users/:id    → Update user
DELETE /api/users/:id    → Delete user
REST API CRUD
NODE
const express = require("express");
const app = express();
app.use(express.json());

let items = [];
let idCounter = 1;

// GET all
app.get("/items", (req, res) => res.json(items));

// GET one
app.get("/items/:id", (req, res) => {
    const item = items.find(i => i.id === +req.params.id);
    item ? res.json(item) : res.status(404).json({ error: "Not found" });
});

// POST
app.post("/items", (req, res) => {
    const item = { id: idCounter++, ...req.body };
    items.push(item);
    res.status(201).json(item);
});

// DELETE
app.delete("/items/:id", (req, res) => {
    items = items.filter(i => i.id !== +req.params.id);
    res.json({ success: true });
});

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental Node.js feature.