app.use()

app.use([path,] function [, function...]) Mounts the specified middleware function or functions at the specified path. If path is not specified, it defaults to “/”. A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. Note that req.originalUrl in a middleware function is a combination of req.baseUrl and req.path, as shown in the following example. app.use('/admin', f

Using template engines with Express

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page. Some popular template engines that work with Express are Pug, Mustache, and EJS. The Express application generator uses Pug as its default, but it also supports several others. See Template Engines (Express wi

res.get()

res.get(field) Returns the HTTP response header specified by field. The match is case-insensitive. res.get('Content-Type'); // => "text/plain"

app.listen()

app.listen(port, [hostname], [backlog], [callback]) Binds and listens for connections on the specified host and port. This method is identical to Node’s http.Server.listen(). var express = require('express'); var app = express(); app.listen(3000); The app returned by express() is in fact a JavaScript Function, designed to be passed to Node’s HTTP servers as a callback to handle requests. This makes it easy to provide both HTTP and HTTPS versions of your app with the same code base, as the app

req.acceptsLanguages()

req.acceptsLanguages(lang [, ...]) Returns the first accepted language of the specified languages, based on the request’s Accept-Language HTTP header field. If none of the specified languages is accepted, returns false. For more information, or if you have issues or concerns, see accepts.

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');

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

app.post()

app.post(path, callback [, callback ...]) Routes HTTP POST requests to the specified path with the specified callback functions. For more information, see the routing guide. You can provide multiple callback functions that behave just like middleware, except that these callbacks can invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current

app.enabled()

app.enabled(name) Returns true if the setting name is enabled (true), where name is one of the properties from the app settings table. app.enabled('trust proxy'); // => false app.enable('trust proxy'); app.enabled('trust proxy'); // => true

res.json()

res.json([body]) Sends a JSON response. This method is identical to res.send() with an object or array as the parameter. However, you can use it to convert other values to JSON, such as null, and undefined (although these are technically not valid JSON). res.json(null); res.json({ user: 'tobi' }); res.status(500).json({ error: 'message' });