liveBook · Manning

Express

As explained in Road to Kubernetes

Express.js is a minimal Node.js web application framework for building server-side applications and APIs.

Express.js is a minimal server-side web framework for Node.js. It helps build web applications with little overhead, using npm to install packages and package.json to track dependencies. section 2.2

A small Express application can:

  • Create an application instance and listen on a chosen port, using PORT when supplied or 3000 by default. section 2.2.1
  • Map an HTTP method and URL path to a callback with the form app.<httpMethod>(<pathString>, <callback>). section 2.2.1
  • Return HTML responses or JSON data from routes. section 2.2.2 section 2.2.3

Hello Express World

Initialize a project with npm init, install Express using npm install express, and create main.js. The application can define app.get('/', (req, res) => res.send("<h1>Hello Express World!</h1>")) and start with app.listen(port, ...). Run it from the source directory with node main.js; set PORT=3011 to test another port. section 2.2 section 2.2.1 section 2.2.2

Listing 2.17 Express.js module in main.js
const express = require('express');    //#1
const fs = require('fs');
const path = require('path')
const app = express();
const port = process.env.PORT || 3000; //#2

app.get('/', (req, res) => {
    res.send("<h1>Hello Express World!</h1>");
})

app.listen(port, () => {
  const appPid = path.resolve(__dirname, 'app.pid')
  fs.writeFileSync(appPid, `${process.pid}`);
  console.log(`Server running on port http://127.0.0.1:${port}`);
})

Inspect the complete main.js example, including imports, application creation, PORT selection, the root route, and startup behavior. section 2.2.1

JSON route

Add a route such as /api/v2/rocket-man1/ that creates an object, converts it with JSON.stringify(), and returns it with res.json(). This gives the application both HTML and JSON response paths. section 2.2.3

How Road to Kubernetes covers it

How deep it goes
Explains project setup, routing, startup, ports, HTML, and JSON responses, while deferring broader Express.js treatment.
Examples
Demonstrates a Hello World route, configurable ports, HTML output, and a JSON API route.
What it assumes
Assumes basic JavaScript familiarity while requiring Node.js 16.14 or higher and npm installation.
What you can do after
Builds a runnable Express application that serves HTML and JSON on configurable local ports.
How it connects
Connects Express.js with Node.js, npm, HTTP methods, routes, JSON, FastAPI, and environment variables.

Also explained in

Compare how 4 books explain Express →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.