Book Image

Express Web Application Development

By : Hage Yaaapa
Book Image

Express Web Application Development

By: Hage Yaaapa

Overview of this book

Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications. It provides a thin layer of features fundamental to any web application, without obscuring features that developers know and love in node.js. "Express Web Application Development" is a comprehensive guide for those looking to learn how to use the Express web framework for web application development. Starting with the initial setup of the Express web framework, "Express Web Application Development" helps you to understand the fundamentals of the framework. By the end of "Express Web Application Development", you will have acquired enough knowledge and skills to create production-ready Express apps. All of this is made possible by the incremental introduction of more advanced topics, starting from the very essentials. On the way to mastering Express for application development, we teach you the more advanced topics such as routes, views, middleware, forms, sessions, cookies and various other aspects of configuring an Express application. Jade; the recommended HTML template engine, and Stylus; the CSS pre-processor for Express, are covered in detail. Last, but definitely not least, Express Web Application Development also covers practices and setups that are required to make Express apps production-ready.
Table of Contents (15 chapters)
Express Web Application Development
About the Author
About the Reviewers

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:



app.set(name, value)

Sets app- specific properties


Retrieves value set by app.set()


Enables a setting in the app


Disables a setting in the app


Checks if a setting is enabled


Checks if a setting is disabled

app.configure([env], callback)

Sets app settings conditionally based on the development environment

app.use([path], function)

Loads a middleware in the app

app.engine(ext, callback)

Registers a template engine for the app

app.param([name], callback)

Adds logic to route parameters

app.VERB(path, [callback...], callback)

Defines routes and handlers based on HTTP verbs

app.all(path, [callback...], callback)

Defines routes and handlers for all HTTP verbs


The object to store variables accessible from any view

app.render(view, [options], callback)

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:




Holds the values of named routes parameters


Returns the value of a parameter from named routes or GET params or POST params


Holds the values of a GET form submission


Holds the values of a POST form submission


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 req.url


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:




Sets the HTTP response code

res.set(field, [value])

Sets response HTTP headers


Gets the response HTTP header

res.cookie(name, value, [options])

Sets cookie on the client

res.clearCookie(name, [options])

Deletes cookie on the client

res.redirect([status], url)

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

res.send([body|status], [body])

Sends an HTTP response object, with an optional HTTP response code

res.json([status|body], [body])

Sends a JSON object for HTTP response, along with an optional HTTP response code

res.jsonp([status|body], [body])

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

res.sendfile(path, [options], [callback]])

Sends a file to the client, [filename], [callback])

Prompts the client to download a file


Sets the HTTP Links header


The object to store variables specific to the view rendering a request

res.render(view, [locals], callback)

Renders a view