http://expressjs.com/migrating-4.html

The routing system

 

 

app.route() method

The new app.route() method enables you to create chainable route handlers for a route path. Since the path is specified in a single location, it helps to create modular routes and reduce redundancy and typos. For more information on routes, see Router() documentation.

新的路由方法app.route可以创建路由队列,用于处理路由路径

Here is an example of chained route handlers defined using app.route().

使用方法

var express = require("express");
var app = express();
app.route('/book')
  .get(function(req, res) {
    res.send('Get a random book');
  })
  .post(function(req, res) {
    res.send('Add a book');
  })
  .put(function(req, res) {
    res.send('Update the book');
  })
  
这样访问 http://localhost:3000/book 时候就是对应的内容了


express.Router class

The other feature to help organize routes is a new class, express.Router, that you can use to create modular mountable route handlers. A Router instance is a complete middleware and routing system; for this reason it is often referred to as a "mini-app".

The following example creates a router as a module, loads a middleware in it, defines some routes, and mounts it on a path on the main app.

Create a router file named birds.js in the app directory, with the following content:

这个类可以让你通过创建router的模块进行处理,Router实例拥有完整的中间件和routing系统,因此也被称为"mini-app"

下面创建一个birds.js的模块 首先建立一个 birds.js文件

var express = require('express');
var router = express.Router();

// middleware specific to this router
router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now());
  next();
})
// define the home page route
router.get('/', function(req, res) {
  res.send('Birds home page');
})
// define the about route
router.get('/about', function(req, res) {
  res.send('About birds');
})

module.exports = router;

然后在你的app.js中进行使用

var birds = require('./birds');
...
app.use('/birds', birds);

访问 http://localhost:3000/birds 和 http://localhost:3000/birds/about 会发现中间件

timeLog被调用了,它返回的结果在log中显示了出来
 

Other changes

The following table lists other small but important changes in Express 4.

 

ObjectDescription
Node Express 4 requires Node 0.10.x or later and has dropped support for0.8.x.
http.createServer() The http module is no longer needed. The app is started usingapp.listen().
app.configure() app.configure() has been removed. Use process.env.NODE_ENV orapp.get('env') to detect the environment and configure the app accordingly.
json spaces The json spaces application property is disabled by default in Express 4.
req.accepted() Use req.accepts()req.acceptsEncodings(),req.acceptsCharsets(), and req.acceptsLanguages().
res.location() No longer resolves relative URLs.
req.params Was an array, is now an object.
res.locals Was a function, is now an object.
res.headerSent Changed to res.headersSent.
app.route Now available as app.mountpath.
res.on('header') Removed.
res.charset Removed.
res.setHeader('Set-Cookie', val) Functionality is now limited to setting the basic cookie value. Use res.cookie() for added functionality.

 
 posted on 2014-08-26 22:32  大家嗨起来  阅读(290)  评论(0)    收藏  举报