Technology

Building a Minimal API with Express.js

Beginner 17 min read VisTechie Team Technology
1 views

Starting from a single server file

Every Express app starts more or less the same way - create an app instance, tell it to parse JSON bodies, and start listening on a port.

import express from "express";

const app = express();
app.use(express.json());

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Splitting routes into their own files

Once you have more than two or three endpoints, keeping everything in one file gets messy fast. Move related routes into their own router.

// routes/posts.js
import { Router } from "express";
const router = Router();

router.get("/", async (req, res) => {
  const posts = await Post.find({}).lean();
  res.json({ success: true, data: posts });
});

router.post("/", async (req, res) => {
  const post = await Post.create(req.body);
  res.status(201).json({ success: true, data: post });
});

export default router;
// app.js
import postsRouter from "./routes/posts.js";
app.use("/api/posts", postsRouter);

Middleware is just a function with three arguments

A logging middleware, an auth check, a rate limiter - they're all the same shape: a function that receives the request, the response, and a way to hand off to whatever comes next.

function requestLogger(req, res, next) {
  console.log(`${req.method} ${req.originalUrl}`);
  next();
}

app.use(requestLogger);

One error handler at the end, not scattered try/catches everywhere

Express lets you define a single error-handling middleware with four arguments. Wrap your route logic and pass caught errors to next(err) instead of repeating the same try/catch boilerplate in every route.

app.use((err, req, res, next) => {
  console.error(err);
  res.status(err.status || 500).json({
    success: false,
    message: err.message || "Internal server error",
  });
});
1 views

Comments

0/2000

Comments are reviewed before being published.

Loading comments...

Related Articles