req.route

req.route Contains the currently-matched route, a string. For example: app.get('/user/:id?', function userIdHandler(req, res) { console.log(req.route); res.send('GET'); }); Example output from the previous snippet: { path: '/user/:id?', stack: [ { handle: [Function: userIdHandler], name: 'userIdHandler', params: undefined, path: undefined, keys: [], regexp: /^\/?$/i, method: 'get' } ], methods: { get: true } }

Moving to Express 5

Overview Express 5.0 is still in the alpha release stage, but here is a preview of the changes that will be in the release and how to migrate your Express 4 app to Express 5. Express 5 is not very different from Express 4: The changes to the API are not as significant as from 3.0 to 4.0. Although the basic API remains the same, there are still breaking changes; in other words an existing Express 4 program might not work if you update it to use Express 5. To install the latest alpha and to previ

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.subdomains

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

FAQ

How should I structure my application? There is no definitive answer to this question. The answer depends on the scale of your application and the team that is involved. To be as flexible as possible, Express makes no assumptions in terms of structure. Routes and other application-specific logic can live in as many files as you wish, in any directory structure you prefer. View the following examples for inspiration: Route listings Route map MVC style controllers Also, there are third-party ex

Database integration

Adding the capability to connect databases to Express apps is just a matter of loading an appropriate Node.js driver for the database in your app. This document briefly explains how to add and use some of the most popular Node.js modules for database systems in your Express app: Cassandra CouchDB LevelDB MySQL MongoDB Neo4j PostgreSQL Redis SQLite ElasticSearch These database drivers are among many that are available. For other options, search on the npm site. Cassandra Module: cassandra-dr

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

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' });

req.ips

req.ips When the trust proxy setting does not evaluate to false, this property contains an array of IP addresses specified in the X-Forwarded-For request header. Otherwise, it contains an empty array. This header can be set by the client or by the proxy. For example, if X-Forwarded-For is client, proxy1, proxy2, req.ips would be ["client", "proxy1", "proxy2"], where proxy2 is the furthest downstream.

req.xhr

req.xhr A Boolean property that is true if the request’s X-Requested-With header field is “XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery. req.xhr // => true