diff --git a/docs/guide.html b/docs/guide.html index c346f560a8..f185aa92c4 100644 --- a/docs/guide.html +++ b/docs/guide.html @@ -340,6 +340,8 @@

Settings

  • views Root views directory defaulting to CWD/views
  • view engine Default view engine name for views rendered without extensions
  • view options An object specifying global view options
  • +
  • view cache Enable view caching (enabled in production)
  • +
  • case sensitive routes Enable case-sensitive routing
  • @@ -355,7 +357,7 @@

    Routing

    }); -

    A route is simply a string which is compiled to a RegExp internally. For example +

    A route is simple a string which is compiled to a RegExp internally. For example when /user/:id is compiled, a simplified version of the regexp may look similar to:

    \/user\/([^\/]+)\/?
    @@ -431,7 +433,7 @@ 

    Routing

    app.listen(3000);
    -

    Typically we may use a “dumb” placeholder such as “/user/:id” which has no restrictions, however say for example we are limiting a user id to digits, we may use ‘/user/:id(\d+)’ which will not match unless the placeholder value contains only digits.

    +

    Typically we may use a “dumb” placeholder such as “/user/:id” which has no restrictions, however say for example we are limiting a user id to digits, we may use ‘/user/:id([0-9]+)’ which will not match unless the placeholder value contains only digits.

    Passing Route Control

    @@ -519,6 +521,34 @@

    Middleware

    app.use(express.bodyParser()); +

    Middleware ordering is important, when Connect receives a request the first middleware we pass to createServer() or use() is executed with three parameters, request, response, and a callback function usually named next. When next() is invoked the second middleware will then have it’s turn and so on. This is important to note because many middleware depend on each other, for example methodOverride() checks req.body.method for the HTTP method override, however bodyParser() parses the request body and populates req.body. Another example of this is cookie parsing and session support, we must first use() cookieParser() followed by session()_.

    + +

    Many Express applications may contain the line app.use(app.router), while this may appear strange, it’s simply the middleware function that contains all defined routes, and performs route lookup based on the current request url and HTTP method. Express allows you to position this middleware, though by default it will be added to the bottom. By positioning the router, we can alter middleware precedence, for example we may want to add error reporting as the last middleware so that any exception passed to next() will be handled by it, or perhaps we want static file serving to have low precedence, allowing our routes to intercept requests to a static file to count downloads etc. This may look a little like below

    + +
    app.use(express.logger(...));
    +app.use(express.bodyParser(...));
    +app.use(express.cookieParser(...));
    +app.use(express.session(...));
    +app.use(app.router);
    +app.use(express.static(...));
    +app.use(express.errorHandler(...));
    +
    + +

    First we add logger() so that it may wrap node’s req.end() method, providing us with response-time data. Next the request’s body will be parsed (if any), followed by cookie parsing and session support, meaning req.session will be defined by the time we hit our routes in app.router. If a request such as GET /javascripts/jquery.js is handled by our routes, and we do not call next() then the static() middleware will never see this request, however if were to define a route as shown below, we can record stats, refuse downloads, consume download credits etc.

    + +
    var downloads = {};
    +
    +app.use(app.router);
    +app.use(express.static(__dirname + '/public'));
    +
    +app.get('/*', function(req, res, next){
    +  var file = req.params[0];
    +  downloads[file] = downloads[file] || 0;
    +  downloads[file]++;
    +  next();
    +});
    +
    +

    Route Middleware

    Routes may utilize route-specific middleware by passing one or more additional callbacks (or arrays) to the method. This feature is extremely useful for restricting access, loading data used by the route etc.

    @@ -595,6 +625,8 @@

    Route Middleware

    For this example in full, view the route middleware example in the repository.

    +

    There are times when we may want to “skip” passed remaining route middleware, but continue matching subsequent routes. To do this we invoke next() with the string “route” next('route'). If no remaining routes match the request url then Express will respond with 404 Not Found.

    +

    HTTP Methods

    We have seen app.get() a few times, however Express also exposes other familiar HTTP verbs in the same manor, such as app.post(), app.del(), etc.

    @@ -1155,7 +1187,7 @@

    res.sendfile(path[, options[, callback]])

    }); -

    res.download(file[, filename[, callback]])

    +

    res.download(file[, filename[, callback[, callback2]]])

    Transfer the given file as an attachment with optional alternative filename.

    @@ -1169,13 +1201,22 @@

    res.download(file[, filename[, callback]])

    res.sendfile(file); -

    An optional callback may be supplied as either the second or third argument, which is passed to res.sendfile():

    +

    An optional callback may be supplied as either the second or third argument, which is passed to res.sendfile(). Within this callback you may still respond, as the header has not been sent.

    res.download(path, 'expenses.doc', function(err){
       // handle
     });
     
    +

    An optional second callback, callback2 may be given to allow you to act on connection related errors, however you should not attempt to respond.

    + +
    res.download(path, function(err){
    +  // error or finished
    +}, function(err){
    +  // connection related error
    +});
    +
    +

    res.send(body|status[, headers|status[, status]])

    The res.send() method is a high level response utility allowing you to pass @@ -1233,7 +1274,7 @@

    res.cookie(name, val[, options])

    }); -

    res.clearCookie(name)

    +

    res.clearCookie(name[, options])

    Clear cookie name by setting “expires” far in the past.

    @@ -1525,9 +1566,7 @@

    app.helpers(obj)

    - `settings`  the app's settings object
     - `filename`  the view's filename
    -- `request`   the request object
    -- `response`  the response object
    -- `app`       the application itself
    +- `layout(path)`  specify the layout from within a view
     

    This method is aliased as app.locals().