Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Express Web Application Development

You're reading from   Express Web Application Development Here's a comprehensive guide to making the most of Express's flexibility in building web applications. With lots of screenshots and examples, it's the perfect step-by-step manual for those with an intermediate knowledge of JavaScript.

Arrow left icon
Product type Paperback
Published in Jun 2013
Publisher Packt
ISBN-13 9781849696548
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Hage Yaaapa Hage Yaaapa
Author Profile Icon Hage Yaaapa
Hage Yaaapa
Arrow right icon
View More author details
Toc

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

You have been reading a chapter from
Express Web Application Development
Published in: Jun 2013
Publisher: Packt
ISBN-13: 9781849696548
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime