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

res.location()

res.location(path) Sets the response Location HTTP header to the specified path parameter. res.location('/foo/bar'); res.location('http://example.com'); res.location('back'); A path value of “back” has a special meaning, it refers to the URL specified in the Referer header of the request. If the Referer header was not specified, it refers to “/”. Express passes the specified URL string as-is to the browser in the Location header, without any validation or manipulation, except in case of back.

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

Events

Events

req.secure

req.secure A Boolean property that is true if a TLS connection is established. Equivalent to: 'https' == req.protocol;

app.engine()

app.engine(ext, callback) Registers the given template engine callback as ext. By default, Express will require() the engine based on the file extension. For example, if you try to render a “foo.pug” file, Express invokes the following internally, and caches the require() on subsequent calls to increase performance. app.engine('pug', require('pug').__express); Use this method for engines that do not provide .__express out of the box, or if you wish to “map” a different extension to the templat

app.param()

app.param([name], callback) Add callback triggers to route parameters, where name is the name of the parameter or an array of them, and callback is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, the value of the parameter and the name of the parameter, in that order. If name is an array, the callback trigger is registered for each parameter declared in it, in the order in which they are declared. Furthermore, for

Serving static files in Express

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly. For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public: app.use(express.static('public')); Now, you can load the files that are in the public directory: http://loc

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

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: