app.disable()

app.disable(name) Sets the Boolean setting name to false, where name is one of the properties from the app settings table. Calling app.set('foo', false) for a Boolean property is the same as calling app.disable('foo'). For example: app.disable('trust proxy'); app.get('trust proxy'); // => false

app.locals

app.locals The app.locals object has properties that are local variables within the application. app.locals.title // => 'My App' app.locals.email // => 'me@myapp.com' Once set, the value of app.locals properties persist throughout the life of the application, in contrast with res.locals properties that are valid only for the lifetime of the request. You can access local variables in templates rendered within the application. This is useful for providing helper functions to templates, as

res.links()

res.links(links) Joins the links provided as properties of the parameter to populate the response’s Link HTTP header field. For example, the following call: res.links({ next: 'http://api.example.com/users?page=2', last: 'http://api.example.com/users?page=5' }); Yields the following results: Link: <http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last"

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

req.param()

req.param(name [, defaultValue]) Deprecated. Use either req.params, req.body or req.query, as applicable. Returns the value of param name when present. // ?name=tobi req.param('name') // => "tobi" // POST name=tobi req.param('name') // => "tobi" // /user/tobi for /user/:name req.param('name') // => "tobi" Lookup is performed in the following order: req.params req.body req.query Optionally, you can specify defaultValue to set a default value if the parameter is not found in any o

Installing

Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory. $ mkdir myapp $ cd myapp Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling. $ npm init This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for mos

res.redirect()

res.redirect([status,] path) Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”. res.redirect('/foo/bar'); res.redirect('http://example.com'); res.redirect(301, 'http://example.com'); res.redirect('../login'); Redirects can be a fully-qualified URL for redirecting to a different site: res.redirect('http://google.com'); Redirects can be relative to the root o

Production best practices: performance and reliability

Overview This article discusses performance and reliability best practices for Express applications deployed to production. This topic clearly falls into the “devops” world, spanning both traditional development and operations. Accordingly, the information is divided into two parts: Things to do in your code (the dev part). Things to do in your environment / setup (the ops part). Things to do in your code Here are some things you can do in your code to improve your application’s performance

res.jsonp()

res.jsonp([body]) Sends a JSON response with JSONP support. This method is identical to res.json(), except that it opts-in to JSONP callback support. res.jsonp(null); // => null res.jsonp({ user: 'tobi' }); // => { "user": "tobi" } res.status(500).jsonp({ error: 'message' }); // => { "error": "message" } By default, the JSONP callback name is simply callback. Override this with the jsonp callback name setting. The following are some examples of JSONP responses using the same code: /