@kexinWeb
2017-02-22T11:43:06.000000Z
字数 3607
阅读 3264
express.router
node
从Express4.0开始,路由Express.Router作为一个单独的组件独立出来。它像一个小型的express实例app一样,同样拥有自己的use、get、post方法。
//app.js
// 加载express模块后无需加载http模块,express模块本身就是对http模块的封装与拓展
const express = require('express');
//创建一个express实例
let app = express();
app.get('/', function(req, res) {
res.send('This is home page.');
});
app.get('/article', function(req, res) {
res.send('This is article page');
});
app.get('/record', function(req, res) {
res.send('This is record page');
});
app.listen(8123);
console.log('The server running on 8123');
//app.js
// 加载express模块
const express = require('express');
//创建一个express实例
let app = express();
//创建一个express.router实例
let router = express.Router();
router.get('/', function(req, res) {
res.send('This is home page.');
});
router.get('/article', function(req, res) {
res.send('This is article page');
});
router.get('/record', function(req, res) {
res.send('This is record page');
});
app.use('/', router);
app.listen(8123);
console.log('The server running on 8123');
乍看没有什么区别,用router有什么好处呢?
关于Express.Router最关键的一点就是最后一行代码:
app.use('/', router);
它的意思就是把路由器实例router挂载到根目录'/'上,所以你访问'/','/acticle','/record'会正确route到目的页面。
如果把这一行代码修改为:
app.use('/blog', router);
那么访问路径就应该相应改变为'/blog/','/blog/article','/blog/record'。
- 一个根路径可以挂载多个Express.Router实例
//app.js
// 加载express模块
const express = require('express');
//创建一个express实例
let app = express();
//创建express.router实例
let router1 = express.Router();
let router2 = express.Router();
//router1
router1.get('/', function(req, res) {
res.send('This is home page.');
});
router1.get('/article', function(req, res) {
res.send('This is article page');
});
router1.get('/record', function(req, res) {
res.send('This is record page');
});
//router2
router2.get('/admin', function(req, res) {
res.send('This is admin page.');
});
router2.get('/user', function(req, res) {
res.send('This is user page');
});
//router1和router2实例挂载到根目录
app.use('/', router1);
app.use('/', router2);
app.listen(8123);
console.log('The server running on 8123');
//app.js
// 加载express模块
const express = require('express');
//创建一个express实例
let app = express();
//创建express.router实例
let router = express.Router();
router.get('/', function(req, res) {
res.send('This is home page.');
});
router.get('/article', function(req, res) {
res.send('This is article page');
});
router.get('/record', function(req, res) {
res.send('This is record page');
});
//router实例挂载到'/admin'目录
app.use('/admin', router);
//router实例挂载到'/user'目录
app.use('/user', router);
app.listen(8123);
console.log('The server running on 8123');
//app.js
// 加载express模块
const express = require('express');
//创建一个express实例
let app = express();
app.get('/admin/', function(req, res) {
res.send('This is home page.');
});
app.get('/admin/article', function(req, res) {
res.send('This is article page');
});
app.get('/admin/record', function(req, res) {
res.send('This is record page');
});
app.listen(8123);
console.log('The server running on 8123');
需要逐个路径配置,结构比较混乱,而改用Express.Router之后
//app.js
// 加载express模块
const express = require('express');
//创建一个express实例
let app = express();
//创建express.router实例
let router = express.Router();
router.get('/', function(req, res) {
res.send('This is home page.');
});
router.get('/article', function(req, res) {
res.send('This is article page');
});
router.get('/record', function(req, res) {
res.send('This is record page');
});
//router实例挂载到'/admin'目录
app.use('/admin', router);
app.listen(8123);
console.log('The server running on 8123');
var router = express.Router();
router.route('/')
.post(function(req, res) {
// ...
})
.get(function(req, res) {
//...
});
});
app.use('/', router);
从上面例子可以看到,Express.Router实例router的route方法,可以对同一路径配置不同的Http动词方法,由此简化了编程。
总而言之,一项技术的出现总该有它出现的理由以及进步的地方,理解这些地方是学好这一项技术的好方法。