The stuff that makes up Express
A good thing about Express is that there are only three core components to it, which makes it relatively easy to know a lot about Express, if not master it entirely. In this section, I will give a brief introduction about each of the core Express components, so that you are not left disoriented when you come across them in the coming chapters.
The application object
The application object is an instance of Express, conventionally represented by the variable named app
. This is the main object of your Express app and the bulk of the functionality is built on it.
This is how you create an instance of the Express module:
var express = require('express'); var app = new express();
The following is a brief description of all the properties and methods available on the application object:
Property/Method |
Description |
---|---|
|
Sets app- specific properties |
|
Retrieves
value set by |
|
Enables a setting in the app |
|
Disables a setting in the app |
|
Checks if a setting is enabled |
|
Checks if a setting is disabled |
|
Sets app settings conditionally based on the development environment |
|
Loads a middleware in the app |
|
Registers a template engine for the app |
|
Adds logic to route parameters |
|
Defines routes and handlers based on HTTP verbs |
|
Defines routes and handlers for all HTTP verbs |
|
The object to store variables accessible from any view |
|
Renders view from the app |
|
A list of routes defined in the app |
|
Binds and listen for connections |
The request object
The HTTP request object is created when a client makes a request to the Express app. The object is conventionally represented by a variable named req
, which contains a number of properties and methods related to the current request.
The following table lists all the properties and methods of the req
object and provides a brief description of them:
Property/Method |
Description |
---|---|
|
Holds the values of named routes parameters |
|
Returns the
value of a parameter from named routes or |
|
Holds the
values of a |
|
Holds the
values of a |
|
Holds the files uploaded via a form |
|
Provides details about the current matched route |
|
Cookie values |
|
Signed cookie values |
|
Gets the request HTTP header |
|
Checks if the client accepts the media types |
|
A list of accepted media types by the client |
|
Checks if the incoming request is of the particular media type |
|
The IP address of the client |
|
The IP address of the client, along with that of the proxies it is connected through |
|
The request path |
|
Hostname from the HTTP header |
|
Checks if the request is still fresh |
|
Checks if the request is stale |
|
Checks if the request came via an AJAX request |
|
The protocol used for making the request |
|
Checks if it is a secure connection |
|
Subdomains of the host domain name |
|
The request path, along with any query parameters |
|
Used as a
backup for |
|
A list of accepted languages by the client |
|
Checks if the client accepts the language |
|
A list of accepted charsets by the client |
|
Checks if the client accepts the charset |
The response object
The response object is created along with the request object, and is conventionally represented by a variable named res
. While it may sound a little strange that both of them should be created together, it is a necessity to give all the middlewares a chance to work on the request and the response object, before passing the control to the next middleware.
The following is a table of properties and methods on the response object:
Property/Method |
Description |
---|---|
|
Sets the HTTP response code |
|
Sets response HTTP headers |
|
Gets the response HTTP header |
|
Sets cookie on the client |
|
Deletes cookie on the client |
|
Redirects the client to a URL, with an optional HTTP status code |
|
The location value of the response HTTP header |
|
The charset value of the response HTTP header |
|
Sends an HTTP response object, with an optional HTTP response code |
|
Sends a JSON object for HTTP response, along with an optional HTTP response code |
|
Sends a JSON object for HTTP response with JSONP support, along with an optional HTTP response code |
|
Sets the media type HTTP response header |
|
Sends a response conditionally, based on the request HTTP Accept header |
|
Sets response HTTP header Content-Disposition to attachment |
|
Sends a file to the client |
|
Prompts the client to download a file |
|
Sets the HTTP Links header |
|
The object to store variables specific to the view rendering a request |
|
Renders a view |