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

res.format()

res.format(object) Performs content-negotiation on the Accept HTTP header on the request object, when present. It uses req.accepts() to select a handler for the request, based on the acceptable types ordered by their quality values. If the header is not specified, the first callback is invoked. When no match is found, the server responds with 406 “Not Acceptable”, or invokes the default callback. The Content-Type response header is set when a callback is selected. However, you may alter this wi

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.

req.app

req.app This property holds a reference to the instance of the Express application that is using the middleware. If you follow the pattern in which you create a module that just exports a middleware function and require() it in your main file, then the middleware can access the Express instance via req.app For example: //index.js app.get('/viewdirectory', require('./mymiddleware.js')) //mymiddleware.js module.exports = function (req, res) { res.send('The views directory is ' + req.app.get('v

app.disabled()

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

res.set()

res.set(field [, value]) Sets the response’s HTTP header field to value. To set multiple fields at once, pass an object as the parameter. res.set('Content-Type', 'text/plain'); res.set({ 'Content-Type': 'text/plain', 'Content-Length': '123', 'ETag': '12345' }); Aliased as res.header(field [, value]).

app.on()

app.on('mount', callback(parent)) The mount event is fired on a sub-app, when it is mounted on a parent app. The parent app is passed to the callback function. NOTE Sub-apps will: Not inherit the value of settings that have a default value. You must set the value in the sub-app. Inherit the value of settings with no default value. For details, see Application settings. var admin = express(); admin.on('mount', function (parent) { console.log('Admin Mounted'); console.log(parent); // refer

req.path

req.path Contains the path part of the request URL. // example.com/users?sort=desc req.path // => "/users" When called from a middleware, the mount point is not included in req.path. See app.use() for more details.

req.hostname

req.hostname Contains the hostname derived from the Host HTTP header. When the trust proxy setting does not evaluate to false, this property will instead have the value of the X-Forwarded-Host header field. This header can be set by the client or by the proxy. // Host: "example.com:3000" req.hostname // => "example.com"

Using middleware

Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls. Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next. Middleware functions can perform the following tasks: Execute any co