新手的 express4.0 笔记

本文深入探讨 Express 4 中的全新路由方法 app.route 和 express.Router 类的使用,展示如何创建链式路由处理器,实现模块化路由管理,减少冗余和错误。同时,介绍如何将路由模块化为独立的 express.Router 实例,以及如何在主应用中挂载这些模块。文章还列举了其他关键更新,如 Node 版本要求、http.createServer 的替代方案、环境检测方式变更等。
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
NodeExpress 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 spacesThe 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.paramsWas an array, is now an object.
res.localsWas a function, is now an object.
res.headerSentChanged to res.headersSent.
app.routeNow available as app.mountpath.
res.on('header')Removed.
res.charsetRemoved.
res.setHeader('Set-Cookie', val)Functionality is now limited to setting the basic cookie value. Use res.cookie() for added functionality.

 

转载于:https://www.cnblogs.com/feelsohi/p/3938309.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值