An Express Primer for Beginners
Tags: express, node, server
Published: 2020-05-05 23:44:23

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](https://dev-to-uploads.s3.amazonaws.com/i/b7phjdo4j6cewqiqyoe3.png)
Photo courtesy of Chua Hock-Chuan at [ntu.edu](https://www.ntu.edu.sg/home/ehchua/programming/webprogramming/http_basics.html)

#Getting Familiar with the Express API reference documentation


The [Express API reference](https://expressjs.com/en/4x/api.html) 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](https://expressjs.com/en/4x/api.html#express).

##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](https://expressjs.com/en/4x/api.html#app).

##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] (https://expressjs.com/en/api.html#req).

##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](https://alligator.io/nodejs/res-object-in-expressjs/), 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] (https://expressjs.com/en/api.html#res)

##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](https://expressjs.com/en/api.html#router).