Learning Express: Part 1: Request and Response

Learning Express

Express is a popular minimalist web framework for Node.js.


Installing

Like most Node.js packages, Express is installed through running the following command:

npm install express --save

Once installed, it can be require()‘d or import‘d, depending on ES5 or ES6 usage.

Request and Response

The HyperText Transfer Protocol (HTTP) is based on a communication model (client-server model) where a client sends a request and a server sends a response as a result. These move back and forth in a constant motion where one side sends a message and another responds.

Often, this exchange is described as if the client-server model were a game. Like in checkers or chess, for example, one side “makes a move” and then another side “makes a move.” One side, a server, for example, cannot make a move until the client does first. In a technical sense, this is not strictly equal, as a client can send a request to another server or using a different port number to the same server while it waits for a response, but it is a helpful metaphor.

For all communication, Express, uses these two concepts, request and response.

Setting up Express

To start, Express needs the minimum of two things:

const express = require('express');
const app = express();
const port = 3000;

app.listen(port, () => console.log(`Example app listening on port ${port}!`));


Note: If a port number is not supplied (the value ‘0’ is used), Express will use an arbitrary one.

However, while the minimum two things to start Express can get it running, very little will happen as a result. In order to respond to a client, it needs more information. Specifically, it needs to know how to listen for incoming requests and what it should listen for. These are the HTTP request methods and routes.

To create a simple web server that responds to all request methods for the root, “/”, route, for example, the code would look like the following:

const express = require('express');
const app = express();
const port = 3000;

app.all('/', (request, response) => response.send('Hello World!'));

app.listen(port, () => console.log(`Example app listening on port ${port}!`));


Through telling the app how to respond (what it should send as a response), the incoming request can be replied to from the server.

(request, response)

For all specified routes, Express passes along Request and Response objects populated with values to the supplied callback function.

For example, to learn from what host the incoming request came from, the property request.headers.host can be examined. (As Express extends the built-in HTTP module in Node.js, the headers object is technically part of that object.)

const express = require('express');
const app = express();
const port = 3000;

app.all('/', (request, response) => {
    console.log(request.headers.host);
    response.send("Hello!");
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));