Most of the backend work we do now for client dashboards and mobile companion apps takes the same basic shape: a Node.js service, Express handling routing, and a JSON API in front of a database. None of the individual pieces are complicated. What makes an API pleasant or painful to consume is consistency - the same status codes, the same response shape, the same error format, every time.
Setting up routes that mean what they say
const express = require('express');
const app = express();
app.use(express.json());
app.get('/api/projects', async (req, res) => {
const projects = await Project.find();
res.json(projects);
});
app.post('/api/projects', async (req, res) => {
const project = await Project.create(req.body);
res.status(201).json(project);
});
REST maps HTTP verbs onto actions: GET reads, POST creates, PUT/PATCH updates, DELETE removes. Keeping that mapping honest - never a GET that deletes a record - is what makes an API predictable to anyone reading the route list for the first time.
Status codes are part of the interface
A response body that says {"error": "not found"} with a 200 status forces every client to parse the body just to know what happened. Status codes exist so they don't have to:
app.get('/api/projects/:id', async (req, res) => {
const project = await Project.findById(req.params.id);
if (!project) return res.status(404).json({ error: 'Project not found' });
res.json(project);
});
Centralised error handling
Express supports error-handling middleware - a function with four arguments instead of three - which keeps try/catch blocks out of every single route:
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({ error: err.message || 'Server error' });
});
Versioning before you need it
Prefixing routes with /api/v1/ from day one costs nothing and saves a painful migration the first time a breaking change is unavoidable and an older mobile app build is still out in the wild depending on the old shape.
An API is a promise to whoever calls it. Consistency is how you keep that promise cheap to maintain.
Beyond that: validate input before it touches the database, never trust a client-supplied ID without checking it belongs to the authenticated user, and log enough to debug a production issue without logging anything a user would consider private.
Resource-oriented routes, not verb-in-the-URL routes
app.get('/api/posts', listPosts);
app.get('/api/posts/:id', getPost);
app.post('/api/posts', createPost);
app.put('/api/posts/:id', updatePost);
app.delete('/api/posts/:id', deletePost);
The HTTP verb carries the action; the URL names the resource. Routes like /api/getPosts or /api/deletePost aren't wrong exactly, but they duplicate information the HTTP method already provides and make an API harder to predict for anyone consuming it for the first time.
Middleware is where the real structure lives
Authentication checks, request validation, and error handling belong in middleware functions applied once and reused across routes, rather than repeated inside every handler. A single centralized error handler at the end of the middleware chain - catching anything thrown or passed to next(err) - is what keeps error responses consistent across dozens of endpoints instead of each one improvising its own format.