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.

res.send()

res.send([body]) Sends the HTTP response. The body parameter can be a Buffer object, a String, an object, or an Array. For example: res.send(new Buffer('whoop')); res.send({ some: 'json' }); res.send('<p>some html</p>'); res.status(404).send('Sorry, we cannot find that!'); res.status(500).send({ error: 'something blew up' }); This method performs many useful tasks for simple non-streaming responses: For example, it automatically assigns the Content-Length HTTP response header field

req.accepts()

req.accepts(types) Checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method returns the best match, or if none of the specified content types is acceptable, returns false (in which case, the application should respond with 406 "Not Acceptable"). The type value may be a single MIME type string (such as “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the bes

app.METHOD()

app.METHOD(path, callback [, callback ...]) Routes an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on. See below for the complete list. For more information, see the routing guide. Express supports the following routing methods corresponding to the HTTP methods of the same names: checkout copy delete get head lock merge mkactivity mkcol move m-search notify

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

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

res.type()

res.type(type) Sets the Content-Type HTTP header to the MIME type as determined by mime.lookup() for the specified type. If type contains the “/” character, then it sets the Content-Type to type. res.type('.html'); // => 'text/html' res.type('html'); // => 'text/html' res.type('json'); // => 'application/json' res.type('application/json'); // => 'application/json' res.type('png'); // => image/png:

res.end()

res.end([data] [, encoding]) Ends the response process. This method actually comes from Node core, specifically the response.end() method of http.ServerResponse. Use to quickly end the response without any data. If you need to respond with data, instead use methods such as res.send() and res.json(). res.end(); res.status(404).end();

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()

app.render()

app.render(view, [locals], callback) Returns the rendered HTML of a view via the callback function. It accepts an optional parameter that is an object containing local variables for the view. It is like res.render(), except it cannot send the rendered view to the client on its own. Think of app.render() as a utility function for generating rendered view strings. Internally res.render() uses app.render() to render views. The local variable cache is reserved for enabling view cache. Set it to