Back to blog
Node.jsExpress

Node.js Express API: Quickstart Guide

17 Feb 20267 min read

Node.js Express API: Quickstart Guide

How to build a simple REST API with Express, including routing, middleware, and error handling.


import express, { Express } from "express";
import cors from "cors";
import helmet from "helmet";
import { routes } from "./routes";
import { errorHandler } from "./middleware/errorHandler";
import { requestLogger } from "./middleware/requestLogger";

export function createApp(): Express {
  const app = express();

  // Security and parsing
  app.use(helmet());
  app.use(cors({ origin: process.env.ALLOWED_ORIGINS?.split(',') }));
  app.use(express.json({ limit: "10mb" }));
  app.use(express.urlencoded({ extended: true }));

  // Logging
  app.use(requestLogger);

  // Routes
  app.use("/api/v1", routes);

  // Health check (outside versioned prefix)
  app.get("/health", (_, res) => res.json({ status: "ok" }));

  // Error handler — must be last
  app.use(errorHandler);

  return app;
}
If this helped you, leave a ❤️

Comments

Coming Soon

Comment section will be available shortly

Have thoughts on this? Reach out — I'd love to chat.

Get in Touch