Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 948f8cf5e8
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 60 lines (48 sloc) 2.07 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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 given, 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(){
      // ...
    })
Something went wrong with that request. Please try again.