Node and Express are the runtime and HTTP framework for your API. Topics here cover bootstrapping the app, parsing JSON bodies, and structured logging—everything that runs before a route handler executes. Master this layer first so later routers and controllers plug into a consistent foundation.

Application-level middleware

Matches Application-level middleware: JSON parsing, logging, and app-wide CORS.

4 topic(s) on this page.

How to initialize an Express application — Basic

Create the Express app instance and register core middleware before defining routes.
Learn: Every Express API starts with a single app instance. Register global middleware (JSON parsing, logging, CORS) before routes so every request is prepared the same way. Keep app.js focused on wiring; move route logic into routers as the project grows.

Supplementary examples

Minimal app with a health check

const express = require("express");
const app = express();

app.get("/health", (req, res) => res.json({ ok: true }));

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Listening on ${PORT}`));

Register JSON parsing before routes

const express = require("express");
const app = express();

app.use(express.json()); // must run before handlers that read req.body

Course example

{
"name": "deploying-the-server",
"version": "1.0.0",
"engines": {
"node": "12.13.0"
},
"description": "",
"main": "server.js",
"scripts": {
"start": "node server",
"server": "nodemon server.js",
"client": "npm start --prefix client"
},
"keywords": [],
"dependencies": {
"express": "^4.17.1",
"cors": "^2.8.5"
},
"devDependencies": {
"nodemon": "^2.0.3"
}
}

Additional references & examples

How to add HTTP request logging — Basic

Register pino-http (or similar) on the app to log each incoming request.
Learn: Request logging answers “what hit the server?” in production. pino-http attaches a logger to each request so handlers can log with context. Place it early in the middleware stack, after body parsers if you log POST bodies sparingly.

Supplementary examples

Attach pino-http to the app

const pinoHttp = require("pino-http");
const logger = require("./config/logger");

app.use(pinoHttp({ logger }));

Log inside a handler with req.log

async function list(req, res, next) {
  req.log.info({ path: req.path }, "listing pastes");
  // ...
}

Course example

const pinoHttp = require('pino-http')
const app = express();
app.use(pinoHttp());
app.use(cors());
app.use(express.json());
// Some code omitted for brevity

Additional references & examples

  • pino-http

    HTTP request logging integration for Express.

  • Express.js guide

    Routing, middleware order, and request/response lifecycle.

How to configure logging — Intermediate

Centralize logger setup in a config module with log level and custom request IDs.
Learn: Centralizing logger setup avoids duplicating log level and formatting rules. Use environment variables for LOG_LEVEL in development vs production, and generate a request ID (e.g. nanoid) so log lines for one request can be correlated.

Supplementary examples

Logger module with level from env

const pino = require("pino");
const { nanoid } = require("nanoid");

module.exports = pino({
  level: process.env.LOG_LEVEL || "info",
  genReqId: () => nanoid(8),
});

Course example

const pinoHttp = require("pino-http");
const { nanoid } = require("nanoid");
const level = process.env.LOG_LEVEL
const nodeEnv = process.env.NODE_ENV || 'development'
const prettyPrint = nodeEnv === "development"
const logger = pinoHttp({
genReqId: (request) => request.headers['x-request-id'] || nanoid(),
level,
prettyPrint
});
module.exports = logger;

Additional references & examples

How to enable CORS — Basic — app-wide

Apply the cors middleware on the Express app so all routes allow cross-origin requests.
Learn: Browsers block cross-origin API calls unless the server sends CORS headers. App-wide CORS is simplest when a single frontend origin talks to your API. Tighten options in production—avoid `origin: '*'` if cookies or credentials matter.

Supplementary examples

Default CORS for all routes

const cors = require("cors");
app.use(cors());

Restrict to one frontend origin

app.use(
  cors({
    origin: process.env.FRONTEND_URL || "http://localhost:3000",
    credentials: true,
  })
);

Course example

const logger = require("./config/logger");
const app = express();
app.use(logger);
app.use(cors());
app.use(express.json());
// Some code omitted for brevity

Additional references & examples