Professional Documents
Culture Documents
Express Addressing
Express Addressing
Express
✖
This translation provided by StrongLoop / IBM .
This document may be out of date with the English documentation. For the latest updates, see the English
documentation .
Addressing
Addressing refers to the definition of application endpoints (URIs) and how they respond to client
requests. For an introduction to addressing, see Basic addressing .
The following code is an example of a very basic route.
// respond with "hello world" when a GET request is made to the homepage
res.send('hello world');
});
route methods
A route method is derived from one of the HTTP methods and attached to an instance of the express.
The following code is an example of the paths that are defined for the GET and POST methods to the
application root.
});
});
Express supports the following routing methods that map to HTTP methods: , get, post, put, head, , dele
te, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactiv
ity, checkout, , , , , and .mergem-searchnotifysubscribeunsubscribepatchsearchconnect
To address methods that become invalid JavaScript variable names, use the bracketed notation. For example,app
['m-search']('/', function ...
There is a special routing method, app.all(), which is not derived from any HTTP method. This method
is used to load middleware functions in one path for all request methods.
In the following example, the handler will be executed for requests to “/secret”, whether you use GET,
POST, PUT, DELETE, or any other HTTP request method supported in the http module .
});
Express uses path-to-regexp to map route paths; see the documentation for path-to-regexp for all the
possibilities for defining route paths. Express Route Tester is a very useful tool for testing basic Express routes,
although it does not support pattern matching.
res.send('root');
});
res.send('about');
});
res.send('random.text');
});
res.send('ab?cd');
});
res.send('ab+cd');
});
This route path will match abcd, abxcd, abRABDOMcd, ab123cd, etc.
res.send('ab*cd');
});
res.send('ab(cd)?e');
});
The characters ?, +, *, and () are subsets of their regular expression counterparts. The hyphen (-) and period (.)
are interpreted literally in string-based paths.
res.send('/a/');
});
This route path will match butterflyand dragonfly, but not butterflyman, dragonfly man, etc.
res.send('/.*fly$/');
});
route handlers
You can provide various callback functions that behave like middleware to handle a request. The only
exception is that these callbacks can be called next('route')to bypass all other route callbacks. You can
use this mechanism to impose preconditions on a route, and then pass control to subsequent routes if
there is no reason to continue with the current route.
Route handlers can be in the form of a function, an array of functions, or combinations of both, as shown
in the following examples.
A single callback function can handle a route. For example:
});
More than one callback function can handle a route (make sure you specify the object next). For example:
next();
});
console.log('CB0');
next();
console.log('CB1');
next();
A combination of standalone functions and function arrays can drive a route. For example:
console.log('CB0');
next();
console.log('CB1');
next();
next();
});
Response Methods
The methods on the response object ( res) in the following table can send a response to the client and
terminate the request/response cycle. If neither of these methods is called from a route handler, the client
request will be left hanging.
Method Description
res.download() Request a file to download.
res.end() Ends the response process.
res.json() Send a JSON response.
res.jsonp() Sends a JSON response with JSONP support.
res.redirect() Redirects a request.
res.render() Represents a view template.
res.send() Send a response of various types.
res.sendFile() Sends a file as a sequence of octets.
res.sendStatus() Sets the response status code and sends its string representation as the response
body.
app.route()
You can create chainable route handlers for a route path using app.route(). Since the path is specified
in a single location, creating modular paths is very useful, as is reducing redundancy and typos. For more
information on routes, see: Router() documentation .
Below is an example of chained route handlers that are defined using app.route().
app.route('/book')
.get(function(req, res) {
})
.post(function(req, res) {
res.send('Add a book');
})
.put(function(req, res) {
});
express.Router
Use the class express.Routerto create mountable and modular route handlers. An instance Routeris a
complete addressing and middleware system; for this reason, it is often referred to as an “applet”.
The following example creates a router as a module, loads a middleware function into it, defines some
routes, and mounts the router module to a path in the main application.
Create a named router file birds.jsin the application directory, with the following content:
next();
});
});
res.send('About birds');
});
module.exports = router;
...
app.use('/birds', birds);
The application will now be able to handle requests to /birdsand /birds/about, as well as invoke the
middleware function timeLogthat is specific to the route.
Star 60,404 Express is a project of the OpenJS Foundation . Fork the website on GitHub.
Copyright © StrongLoop, Inc. and other expressjs.com contributors.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States of America License .
Original text
Contribute a better translation