Back to Blog
Lesson 27 of the Node.js: Build Your First Server & CLI course
Node.jsAugust 14, 20264 min read

RESTful API Patterns: Naming Conventions and Status Codes

Master RESTful API patterns by using plural nouns and standard HTTP status codes. Learn to build predictable, professional APIs with consistent resource paths.

RESTAPI designNode.jsExpressBest Practices
Detailed view of programming code in a dark theme on a computer screen.

Previously in this course, we covered implementing create operations with mongoose and express. Now that your server can save data to MongoDB, it’s time to ensure your interface is predictable. In this lesson, we’ll move beyond "just making it work" to designing an API that follows professional REST conventions.

The Philosophy of Resource-Oriented Design

REST (Representational State Transfer) is not a protocol, but an architectural style. At its core, REST treats everything as a resource. When you build an API, you aren't defining "actions" like getUsers or deleteUser; you are defining paths that represent data collections.

Think of your API as a file system:

  • /users represents the entire collection.
  • /users/123 represents a specific document within that collection.

Why Naming Conventions Matter

Consistency is the primary factor in developer experience (DX). If a frontend developer has to guess whether your endpoint is /get-all-users, /listUsers, or /users, your API is poorly designed.

Rule 1: Use Plural Nouns Always use plural nouns for your resource collections. Even if you are dealing with a single entity, the base path should represent the collection.

  • Good: GET /users, GET /users/123
  • Bad: GET /user, GET /user-list

Rule 2: Hierarchical Paths If a resource belongs to another, nest it logically. For example, if users have posts, the route structure should flow from parent to child.

  • GET /users/123/posts (All posts belonging to user 123)
  • GET /posts/456 (A specific post)

Choosing Correct HTTP Status Codes

Your API communicates the outcome of a request through HTTP status codes. Choosing the correct code is vital because clients (browsers, mobile apps) use these codes to decide how to handle the response.

Status CodeMeaningUse Case
200 OKSuccessStandard response for GET, PUT, or PATCH.
201 CreatedSuccessUsed after a successful POST (resource created).
204 No ContentSuccessUsed after a successful DELETE where no body is returned.
400 Bad RequestClient ErrorPayload is malformed or validation failed.
404 Not FoundClient ErrorResource does not exist at this URL.
500 Server ErrorServer ErrorSomething went wrong internally (e.g., DB crash).

Reference: For a deeper dive into these semantics, see REST API status codes: a semantic guide for api design.

Worked Example: Applying Patterns to Our Project

Let's refactor our existing user routes to follow these conventions. Notice how we shift from "action-oriented" routes to "resource-oriented" routes.

JAVASCRIPT
// Before: Poor convention
app.post(CE9178">'/create-user', ...)
app.get(CE9178">'/get-user/:id', ...)

// After: RESTful convention
const express = require(CE9178">'express');
const router = express.Router();

// GET all users
router.get(CE9178">'/users', (req, res) => {
    // 200 is implicit, but good to be aware of
    res.status(200).json({ data: users });
});

// POST a new user
router.post(CE9178">'/users', (req, res) => {
    // 201 Created is the standard for successful resource creation
    res.status(201).json({ message: "User created" });
});

// DELETE a user
router.delete(CE9178">'/users/:id', (req, res) => {
    // 204 No Content is ideal for successful deletions
    res.status(204).send();
});

Hands-on Exercise

Update your current project’s Express routes to strictly follow these three rules:

  1. Ensure all your primary resource endpoints (e.g., books, tasks, products) are pluralized.
  2. Verify that your POST requests return a 201 status code instead of the default 200.
  3. If you have a DELETE route, update it to return 204 with no response body.

Common Pitfalls

  1. Mixing Verbs and Nouns: A common mistake is adding verbs to the URI, like /users/delete. The HTTP method (DELETE) already tells the server what to do; the URI should only identify the resource.
  2. Over-nesting: Avoid going deeper than two levels (e.g., /users/1/posts/2/comments/3). If your path is too long, it's often better to flatten it or provide a separate resource for the deepest entity.
  3. Using 200 for Everything: Never use 200 for every success. Using 201 for creation and 204 for deletions provides machine-readable semantic information to the client.

FAQ

Q: Should I use versioning in my URL? A: Yes. It is common practice to include the version in the path, like /api/v1/users. This allows you to deploy breaking changes in the future without disrupting existing clients.

Q: What if I need to perform an action that doesn't fit CRUD? A: If you have a specific operation like "reset password," it is acceptable to use a "sub-resource" pattern: /users/123/password-reset.

Recap

We've established that designing RESTful APIs relies on consistent naming and status codes. By using plural nouns for collections, following hierarchical path structures, and returning precise HTTP status codes, you turn a collection of endpoints into a professional, predictable interface.

Up next: We will tackle Advanced Error Handling to ensure our API fails gracefully and informs the client exactly what went wrong.

Similar Posts