res.headersSent

res.headersSent Boolean property that indicates if the app sent HTTP headers for the response. app.get('/', function (req, res) { console.log(res.headersSent); // false res.send('OK'); console.log(res.headersSent); // true });

res.sendStatus()

res.sendStatus(statusCode) Sets the response HTTP status code to statusCode and send its string representation as the response body. res.sendStatus(200); // equivalent to res.status(200).send('OK') res.sendStatus(403); // equivalent to res.status(403).send('Forbidden') res.sendStatus(404); // equivalent to res.status(404).send('Not Found') res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error') If an unsupported status code is specified, the HTTP status is still set

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

router.all()

router.all(path, [callback, ...] callback) This method is just like the router.METHOD() methods, except that it matches all HTTP methods (verbs). This method is extremely useful for mapping “global” logic for specific path prefixes or arbitrary matches. For example, if you placed the following route at the top of all other route definitions, it would require that all routes from that point on would require authentication, and automatically load a user. Keep in mind that these callbacks do not h

req.protocol

req.protocol Contains the request protocol string: either http or (for TLS requests) https. When the trust proxy setting does not evaluate to false, this property will use the value of the X-Forwarded-Proto header field if present. This header can be set by the client or by the proxy. req.protocol // => "http"

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

req.cookies

req.cookies When using cookie-parser middleware, this property is an object that contains cookies sent by the request. If the request contains no cookies, it defaults to {}. // Cookie: name=tj req.cookies.name // => "tj" For more information, issues, or concerns, see cookie-parser.

app.all()

app.all(path, callback [, callback ...]) This method is like the standard app.METHOD() methods, except it matches all HTTP verbs. It’s useful for mapping “global” logic for specific path prefixes or arbitrary matches. For example, if you put the following at the top of all other route definitions, it requires that all routes from that point on require authentication, and automatically load a user. Keep in mind that these callbacks do not have to act as end-points: loadUser can perform a task, t

res.locals

res.locals An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals. This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on. app.use(function(req, res, next){ res.locals.user = req.user; res.locals.authenticated = ! req.user.anonymous; next()

req.body

req.body Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer. The following example shows how to use body-parsing middleware to populate req.body. var app = require('express')(); var bodyParser = require('body-parser'); var multer = require('multer'); // v1.0.5 var upload = multer(); // for parsing multipart/form-data app.use(bodyParser.json()); // for parsing applicati