An Express Primer for Beginners

Nathan B Hankes - May 5 '20 - - Dev Community

Express is a framework for developing software that listens for and responds to HTTP requests over the internet. These requests come from a client, which is software (such as a browser) that can make a request via HTTP.

Metaphorically speaking, Express is like an air traffic controller, or a system that coordinates landing and takeoff as requests are radioed into the command tower. It is the job of the air traffic controller to respond to every request and direct airplanes in and out of their intended destinations in an organized and predictable manner. These airplanes, of course, are HTTP requests for items like a file download, accessing a link to a new page on a website, submitting data into a form, storing that data in a database, and much more.

The software we build in Express is considered a server. So in the pictorial example below, the software we develop in Express fulfills the function of position three:
HTTP Loop
Photo courtesy of Chua Hock-Chuan at ntu.edu

Getting Familiar with the Express API reference documentation

The Express API reference for version 4.x organizes itself in five broad categories, and I will describe each of these below:

-express()
-Application
-Request
-Response
-Router

express()

The express() function is required to create an Express application. This is a top-level function included in the Express module at the time you download Express into the root folder of your project by issuing this command:

npm install express --save

The --save adds Express to the package.JSON file created during the npm init

npm and node must be installed on your system

The express() function has several methods, each of which offers developers several options to use within a given method. For instance, express.static(index). express is the function. static is the method. And (index) is the option unique to the static method. This line of code sends the client the index file within the root folder. The default item it sends is the index.html file.

For a list of each express() method and its options, click here.

Application: The app object

This section of the Express documentation is dedicated to the app object. The app object refers to your Express software itself, which the documentation refers to as the Express application. The app object has properties, events, and methods, each of which has various pre-defined arguments, types, and/or properties.

As a simple example, let's explore the set method for the app object. The Express API reference documentation gives us the following arrangement: app.set(name, value), where we can create a name variable and assign it a value. Below we use the set method to create the name 'title' and assign it the value of 'My Site':

app.set('title', 'My Site')

See if you can understand what the app.get method does below:

app.get('title') // "My Site"

Methods for Express's app object serve developer needs such as routing HTTP requests, configuring third-party Express software (middleware), rendering HTML views and more. For a list of each app object methods click here.

Request: The req object

The req object refers to the HTTP request and allows developers to examine the client request. req.ip will return the remote ip address of the request, for example. With Express's built-in methods developers are able to gather information from requests related to protocol, URLs, cookies, paths, search queries, and much more.

To learn more about the Express req object click here.

Response: The res object

The res object refers to the HTTP response that your Express application sends when it gets a request from the client. According to alligator.io, the res.send method may be the most well-known. This method allows the Express application to respond to the client request with data.

To learn more about the Express res object click here

Router: The Router() object

The Router() object was released with Express Version 4 and allows developers to group route handlers by site sections and access these groups using a shared route-prefix.

To learn more about Express Router click here.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player