req.params

req.params This property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /user/:name, then the “name” property is available as req.params.name. This object defaults to {}. // GET /user/tj req.params.name // => "tj" When you use a regular expression for the route definition, capture groups are provided in the array using req.params[n], where n is the nth capture group. This rule is applied to unnamed wild card matches with string

req.method

req.method Contains a string corresponding to the HTTP method of the request: GET, POST, PUT, and so on.

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"

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

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

req.baseUrl

req.baseUrl The URL path on which a router instance was mounted. The req.baseUrl property is similar to the mountpath property of the app object, except app.mountpath returns the matched path pattern(s). For example: var greet = express.Router(); greet.get('/jp', function (req, res) { console.log(req.baseUrl); // /greet res.send('Konichiwa!'); }); app.use('/greet', greet); // load the router on '/greet' Even if you use a path pattern or a set of path patterns to load the router, the base

req.signedCookies

req.signedCookies When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use. Signed cookies reside in a different object to show developer intent; otherwise, a malicious attack could be placed on req.cookie values (which are easy to spoof). Note that signing a cookie does not make it “hidden” or encrypted; but simply prevents tampering (because the secret used to sign is private). If no signed cookies are sent, the property defaul

res.sendFile()

res.sendFile(path [, options] [, fn]) res.sendFile() is supported by Express v4.8.0 onwards Transfers the file at the given path. Sets the Content-Type response HTTP header field based on the filename’s extension. Unless the root option is set in the options object, path must be an absolute path to the file. The following table provides details on the options parameter. Property Description Default Availability maxAge Sets the max-age property of the Cache-Control header in milliseconds or a

Basic routing

Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched. Route definition takes the following structure: app.METHOD(PATH, HANDLER) Where: app is an instance of express. METHOD is an HTTP request method. PATH is a path on the server. HANDLER is the function executed

app.enable()

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