What is REST in Express.js?
Learn what REST means in Express.js, how HTTP verbs map to CRUD operations, statelessness, routing, and status codes with practical code examples.
Expected Interview Answer
REST (Representational State Transfer) is an architectural style for designing APIs around resources identified by URLs, manipulated using standard HTTP methods, and Express.js is the framework most commonly used to implement RESTful APIs in Node.js.
In Express, you model each resource (e.g. /users, /orders) as a route, and map HTTP verbs to CRUD operations: GET to read, POST to create, PUT/PATCH to update, and DELETE to remove. RESTful design is stateless — each request carries all the information needed, with no server-side session tied to a connection — and responses typically use standard HTTP status codes (200, 201, 404, 500) and JSON payloads. Express's app.get/post/put/delete methods plus express.Router() let you organize resource routes into modular files, and middleware like express.json() parses request bodies. Good REST APIs in Express also version endpoints (e.g. /api/v1/users) and use consistent naming (plural nouns, nested resources) for predictability.
- Predictable, resource-oriented URL structure
- Uses standard HTTP methods and status codes
- Stateless requests simplify scaling and caching
- Express Router enables modular, maintainable routes
- Widely understood convention across client tooling
AI Mentor Explanation
REST in Express is like a well-organized scorecard system where every player has a fixed row (the resource URL) and standard actions — add runs, record a wicket, update strike rate — always mean the same thing across every match (HTTP verbs). Any scorer walking into a new stadium already knows exactly where to look and what each action does, because the format never changes.
Step-by-Step Explanation
Step 1
Identify resources
Model your domain as nouns like /users or /orders rather than action-based endpoints.
Step 2
Map HTTP verbs to CRUD
GET reads, POST creates, PUT/PATCH updates, DELETE removes a resource.
Step 3
Set up Express routes
Use app.get/post/put/delete or express.Router() to define handlers per resource.
Step 4
Parse and validate input
Use express.json() middleware and validation libraries before touching request bodies.
Step 5
Return proper status codes
Respond with 200/201 on success, 400/404 on client errors, 500 on server errors.
Step 6
Version and document the API
Prefix routes with /api/v1 and keep responses consistent for client stability.
What Interviewer Expects
- Defines REST as resource-oriented, stateless HTTP API design
- Maps HTTP verbs correctly to CRUD operations
- Knows Express Router for modular route organization
- Mentions proper status code usage
- Understands statelessness and its scaling implications
Common Mistakes
- Using verbs in URLs (e.g. /getUser) instead of resource nouns
- Returning 200 for every response regardless of outcome
- Storing session state server-side, breaking statelessness
- Not versioning the API, causing breaking changes for clients
Best Answer (HR Friendly)
“REST is a standard way of designing web APIs where each piece of data, like a user or order, has its own URL, and standard actions like GET or POST determine what happens to it. Express.js is the popular Node.js framework used to build these APIs consistently and predictably.”
Code Example
const express = require('express');
const router = express.Router();
router.get('/users', (req, res) => res.json({ users: [] }));
router.post('/users', (req, res) => res.status(201).json({ id: 1, ...req.body }));
router.put('/users/:id', (req, res) => res.json({ id: req.params.id, ...req.body }));
router.delete('/users/:id', (req, res) => res.status(204).end());
module.exports = router;
// GET /users -> 200 { users: [] }
// POST /users -> 201 { id: 1, name: 'Ada' }Follow-up Questions
- What does statelessness mean in REST, and why does it matter?
- How would you version a REST API in Express?
- What's the difference between PUT and PATCH?
- How do you structure nested resources like /users/:id/orders?
- How would you add input validation to a REST endpoint?
MCQ Practice
1. Which HTTP method typically creates a new resource in REST?
POST is conventionally used to create a new resource on the server.
2. What does 'stateless' mean in REST architecture?
Statelessness means each request contains all needed info; the server keeps no client session state.
3. Which Express feature helps organize RESTful routes into modules?
express.Router() creates modular, mountable route handlers for organizing REST endpoints.
Flash Cards
What does REST stand for? — Representational State Transfer — an architectural style for resource-oriented, stateless APIs.
Which verb updates a resource fully? — PUT (PATCH is used for partial updates).
What does express.Router() provide? — A way to modularize and mount groups of RESTful routes.
Why version an API (e.g. /api/v1)? — To evolve the API without breaking existing client integrations.