express.static()

express.static(root, [options]) This is the only built-in middleware function in Express. It serves static files and is based on serve-static. The root argument refers to the root directory from which the static assets are to be served. The file to serve will be determined by combining req.url with the provided root directory. When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs.

express.Router()

express.Router([options]) Creates a new router object. var router = express.Router([options]); The optional options parameter specifies the behavior of the router. Property Description Default Availability caseSensitive Enable case sensitivity. Disabled by default, treating “/Foo” and “/foo” as the same. mergeParams Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. false 4.5.0+ strict Enable st

Express behind proxies

When running an Express app behind a proxy, set (by using app.set()) the application variable trust proxy to one of the values listed in the following table. Although the app will not fail to run if the application variable trust proxy is not set, it will incorrectly register the proxy’s IP address as the client IP address unless trust proxy is configured. Type Value Boolean If true, the client’s IP address is understood as the left-most entry in the X-Forwarded-* header. If false, the app i

Express application generator

Use the application generator tool, express-generator, to quickly create an application skeleton. Install express-generator with the following command: $ npm install express-generator -g Display the command options with the -h option: $ express -h Usage: express [options] [dir] Options: -h, --help output usage information -V, --version output the version number -e, --ejs add ejs engine support (defaults to jade) --hbs add handlebars

Events

Events

Error handling

Define error-handling middleware functions in the same way as other middleware functions, except error-handling functions have four arguments instead of three: (err, req, res, next). For example: app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something broke!'); }); You define error-handling middleware last, after other app.use() and routes calls; for example: var bodyParser = require('body-parser'); var methodOverride = require('method-override');

Developing template engines for Express

Use the app.engine(ext, callback) method to create your own template engine. ext refers to the file extension, and callback is the template engine function, which accepts the following items as parameters: the location of the file, the options object, and the callback function. The following code is an example of implementing a very simple template engine for rendering .ntl files. var fs = require('fs'); // this engine requires the fs module app.engine('ntl', function (filePath, options, callba

Debugging Express

Express uses the debug module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle. debug is like an augmented version of console.log, but unlike console.log, you don’t have to comment out debug logs in production code. Logging is turned off by default and can be conditionally turned on by using the DEBUG environment variable. To see all the internal logs used in Express, set the DEBUG environment

Database integration

Adding the capability to connect databases to Express apps is just a matter of loading an appropriate Node.js driver for the database in your app. This document briefly explains how to add and use some of the most popular Node.js modules for database systems in your Express app: Cassandra CouchDB LevelDB MySQL MongoDB Neo4j PostgreSQL Redis SQLite ElasticSearch These database drivers are among many that are available. For other options, search on the npm site. Cassandra Module: cassandra-dr

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched. Route definition takes the following structure: app.METHOD(PATH, HANDLER) Where: app is an instance of express. METHOD is an HTTP request method. PATH is a path on the server. HANDLER is the function executed