mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
60 lines
2.0 KiB
Plaintext
60 lines
2.0 KiB
Plaintext
section
|
|
h3(id='app.VERB') app.VERB(path, [callback...], callback)
|
|
|
|
p.
|
|
The <code>app.VERB()</code> methods provide the routing functionality
|
|
in Express, where <strong>VERB</strong> is one of the HTTP verbs, such
|
|
as <code>app.post()</code>. Multiple callbacks may be give, all are treated
|
|
equally, and behave just like middleware, with the one exception that
|
|
these callbacks may invoke <code>next('route')</code> to bypass the
|
|
remaining route callback(s). This mechanism can be used to perform pre-conditions
|
|
on a route then pass control to subsequent routes when there is no reason to proceed
|
|
with the route matched.
|
|
|
|
p.
|
|
The following snippet illustrates the most simple route definition possible. Express
|
|
translates the path strings to regular expressions, used internally to match incoming requests.
|
|
Query strings are <em>not</em> considered when peforming these matches, for example "GET /"
|
|
would match the following route, as would "GET /?name=tobi".
|
|
|
|
+js.
|
|
app.get('/', function(req, res){
|
|
res.send('hello world');
|
|
});
|
|
|
|
p.
|
|
Regular expressions may also be used, and can be useful
|
|
if you have very specific restraints, for example the following
|
|
would match "GET /commits/71dbb9c" as well as "GET /commits/71dbb9c..4c084f9".
|
|
|
|
+js.
|
|
app.get(/^\/commits\/(\d+)(?:\.\.(\d+))?$/, function(req, res){
|
|
var from = req.params[0];
|
|
var to = req.params[1] || 'HEAD';
|
|
res.send('commit range ' + from + '..' + to);
|
|
});
|
|
|
|
p.
|
|
Several callbacks may also be passed, useful for re-using middleware
|
|
that load resources, perform validations, etc.
|
|
|
|
+js.
|
|
app.get('/user/:id', user.load, function(){
|
|
// ...
|
|
})
|
|
|
|
p.
|
|
These callbacks may be passed within arrays as well, these arrays are
|
|
simply flattened when passed:
|
|
|
|
+js.
|
|
var middleware = [loadForum, loadThread];
|
|
|
|
app.get('/forum/:fid/thread/:tid', middleware, function(){
|
|
// ...
|
|
})
|
|
|
|
app.post('/forum/:fid/thread/:tid', middleware, function(){
|
|
// ...
|
|
})
|