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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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

app.set(name, value)

Sets app- specific properties

app.get(name)

Retrieves value set by app.set()

app.enable(name)

Enables a setting in the app

app.disable(name)

Disables a setting in the app

app.enabled(name)

Checks if a setting is enabled

app.disabled(name)

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

app.locals

The object to store variables accessible from any view

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

Renders view from the app

app.routes

A list of routes defined in the app

app.listen()

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

req.params

Holds the values of named routes parameters

req.params(name)

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

req.query

Holds the values of a GET form submission

req.body

Holds the values of a POST form submission

req.files

Holds the files uploaded via a form

req.route

Provides details about the current matched route

req.cookies

Cookie values

req.signedCookies

Signed cookie values

req.get(header)

Gets the request HTTP header

req.accepts(types)

Checks if the client accepts the media types

req.accepted

A list of accepted media types by the client

req.is(type)

Checks if the incoming request is of the particular media type

req.ip

The IP address of the client

req.ips

The IP address of the client, along with that of the proxies it is connected through

req.path

The request path

req.host

Hostname from the HTTP header

req.fresh

Checks if the request is still fresh

req.stale

Checks if the request is stale

req.xhr

Checks if the request came via an AJAX request

req.protocol

The protocol used for making the request

req.secure

Checks if it is a secure connection

req.subdomains

Subdomains of the host domain name

req.url

The request path, along with any query parameters

req.originalUrl

Used as a backup for req.url

req.acceptedLanguages

A list of accepted languages by the client

req.acceptsLanguage(langauge)

Checks if the client accepts the language

req.acceptedCharsets

A list of accepted charsets by the client

req.acceptsCharsets(charset)

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

res.status(code)

Sets the HTTP response code

res.set(field, [value])

Sets response HTTP headers

res.get(header)

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

res.location

The location value of the response HTTP header

res.charset

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

res.type(type)

Sets the media type HTTP response header

res.format(object)

Sends a response conditionally, based on the request HTTP Accept header

res.attachment([filename])

Sets response HTTP header Content-Disposition to attachment

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

Sends a file to the client

res.download(path, [filename], [callback])

Prompts the client to download a file

res.links(links)

Sets the HTTP Links header

res.locals

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

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

Renders a view