app.put()

app.put(path, callback [, callback ...]) Routes HTTP PUT requests to the specified path with the specified callback functions. For more information, see the routing guide. You can provide multiple callback functions that behave just like middleware, except that these callbacks can invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current r

app.disable()

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

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

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

Developing template engines for Express

Use the app.engine(ext, callback) method to create your own template engine. ext refers to the file extension, and callback is the template engine function, which accepts the following items as parameters: the location of the file, the options object, and the callback function. The following code is an example of implementing a very simple template engine for rendering .ntl files. var fs = require('fs'); // this engine requires the fs module app.engine('ntl', function (filePath, options, callba

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

Hello world example

This is essentially going to be the simplest Express app you can create. It is a single file app — not what you’d get if you use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories for various purposes. First create a directory named myapp, change to it and run npm init. Then install express as a dependency, as per the installation guide. In the myapp directory, create a file named app.js and add the following

router.route()

router.route(path) Returns an instance of a single route which you can then use to handle HTTP verbs with optional middleware. Use router.route() to avoid duplicate route naming and thus typo errors. Building on the router.param() example above, the following code shows how to use router.route() to specify various HTTP method handlers. var router = express.Router(); router.param('user_id', function(req, res, next, id) { // sample user, would actually fetch from DB, etc... req.user = {

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

req.body

req.body Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer. The following example shows how to use body-parsing middleware to populate req.body. var app = require('express')(); var bodyParser = require('body-parser'); var multer = require('multer'); // v1.0.5 var upload = multer(); // for parsing multipart/form-data app.use(bodyParser.json()); // for parsing applicati