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

Writing middleware for use in Express apps

Overview 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 code. Make changes to the request and the response objects. End the request-response cycle. Call the next middleware in the stack. If the current middleware func

res.json()

res.json([body]) Sends a JSON response. This method is identical to res.send() with an object or array as the parameter. However, you can use it to convert other values to JSON, such as null, and undefined (although these are technically not valid JSON). res.json(null); res.json({ user: 'tobi' }); res.status(500).json({ error: 'message' });

app.enabled()

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

req.subdomains

req.subdomains An array of subdomains in the domain name of the request. // Host: "tobi.ferrets.example.com" req.subdomains // => ["ferrets", "tobi"]

express.static()

express.static(root, [options]) This is the only built-in middleware function in Express. It serves static files and is based on serve-static. The root argument refers to the root directory from which the static assets are to be served. The file to serve will be determined by combining req.url with the provided root directory. When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next middleware, allowing for stacking and fall-backs.

Using template engines with Express

A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page. Some popular template engines that work with Express are Pug, Mustache, and EJS. The Express application generator uses Pug as its default, but it also supports several others. See Template Engines (Express wi

req.acceptsCharsets()

req.acceptsCharsets(charset [, ...]) Returns the first accepted charset of the specified character sets, based on the request’s Accept-Charset HTTP header field. If none of the specified charsets is accepted, returns false. For more information, or if you have issues or concerns, see accepts.

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

app.set()

app.set(name, value) Assigns setting name to value, 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'). Similarly, calling app.set('foo', false) for a Boolean property is the same as calling app.disable('foo'). Retrieve the value of a setting with app.get(). app.set('title', 'My Site'); app.get('title'); // "My Site" Application Settings The following table lists application settings. Not