路由路径和请求方法一起定义了请求的端点,它可以是字符串、字符串模式或者正则表达式。后端在获取路由后,可通过一系列类似中间件的函数去执行事务。

可使用字符串的路由路径:

// 匹配根路径的请求
app.get('/', function (req, res) {
res.send('root');
}); // 匹配 /about 路径的请求
app.get('/about', function (req, res) {
res.send('about');
}); // 匹配 /random.text 路径的请求
app.get('/random.text', function (req, res) {
res.send('random.text');
});

可使用字符串模式的路由路径:

// 匹配 acd 和 abcd
app.get('/ab?cd', function(req, res) {
res.send('ab?cd');
}); // 匹配 /abe 和 /abcde
app.get('/ab(cd)?e', function(req, res) {
res.send('ab(cd)?e');
});

可使用正则表达式的路由路径:

// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
res.send('/.*fly$/');
});

可以为请求处理提供多个回调函数,其行为类似中间件。唯一的区别是这些回调函数有可能调用 next('route') 方法而略过其他路由回调函数。可以利用该机制为路由定义前提条件,如果在现有路径上继续执行没有意义,则可将控制权交给剩下的路径。

路由句柄有多种形式,可以是一个函数、一个函数数组,或者是两者混合,如下所示

使用一个回调函数处理路由:

app.get('/example', function (req, res) {
res.send('Hello from A!');
});

使用多个回调函数处理路由(记得指定 next 对象):

app.get('/example/b', function (req, res, next) {
console.log('response will be sent by the next function ...');
next();
}, function (req, res) {
res.send('Hello from B!');
});

使用回调函数数组处理路由:

var cb0 = function (req, res, next) {
console.log('CB0');
next();
} var cb1 = function (req, res, next) {
console.log('CB1');
next();
} var cb2 = function (req, res) {
res.send('Hello from C!');
} app.get('/example/c', [cb0, cb1, cb2]);

混合使用函数和函数数组处理路由:

var cb0 = function (req, res, next) {
console.log('CB0');
next();
} var cb1 = function (req, res, next) {
console.log('CB1');
next();
} app.get('/example/d', [cb0, cb1], function (req, res, next) {
console.log('response will be sent by the next function ...');
next();
}, function (req, res) {
res.send('Hello from D!');
});

可使用 express.Router 类创建模块化、可挂载的路由句柄。Router 实例是一个完整的中间件和路由系统。

var log=require("./log/log");
var express=require("express");
var app=express(); //首先定义一个路由
var router=express.Router(); //使用中间件
router.use(function firstFunc(req,res,next){
console.log("Time: ",Date.now());
next();
}); //如果是根目录
router.get("/",function(req,res){
res.send("home page");
}); //如果是根目录下的about目录
router.get("/about",function(req,res){
res.send("about page");
}); //使用路由
//可输入http://127.0.0.1:8000/myRoot得到home page
//可输入http://127.0.0.1:8000/myRoot/about得到about page
//在得到返回的page之前,会先执行firstFunc函数
app.use("/myRoot",router); //开启站点
app.listen(,function(){
log.info("站点开启")
});

通过路由,可以在返回页面之前,先通过中间件执行若干事物,而且这些中间件是当前路由共享的,这样可以省去许多重复代码,增加代码可利用率的有效性。还可以将Router注册为模块导出,代码更加有可读性。

注明:以上学习内容来自:http://www.expressjs.com.cn/guide/routing.html

最新文章

  1. 【转】SVN提交一般原则
  2. The property on could not be set to a 'Int16' value.You must set this property to a non-null value of type ‘Int32’.”
  3. apache ab压力测试
  4. DataContractSerializer序列化与反序列化遇到的奇怪问题
  5. [Tool] PowerDesigner
  6. iOS 23 种设计模式
  7. Context Menu on DataGrid
  8. windows 一个进程可以允许最大的线程数
  9. IIS注册asp.net 4.0
  10. 移动开发(webapp)过程中的小细节总结
  11. hdu 2829 Lawrence(斜率优化DP)
  12. ubuntu14.04安装、NVIDIA显卡驱动安装及CUDA8.0、Cudnn5.1的环境搭建
  13. 手把手教你在Ubuntu上分别安装Nginx、PHP和Mysql
  14. 【BZOJ4825】【HNOI2017】单旋(Link-Cut Tree)
  15. Python中区分函数和方法
  16. docker(三) 镜像和容器常用命令
  17. canvas用数组方式做出下雨效果
  18. AtCoder Beginner Contest 122 解题报告
  19. MySQL全备+binlog恢复方法之伪装master【原创】
  20. 洛谷P5219 无聊的水题 I [prufer序列,生成函数,NTT]

热门文章

  1. hdu4719 Oh My Holy FFF 线段树优化dp
  2. fread
  3. perl入门知识(3)
  4. python从字符串解析方法名
  5. UVa 1606 两亲性分子
  6. python 获取当前时间戳
  7. jquery作业 教授答案
  8. Qt_OpenGL_教程
  9. (转)C#制作一个消息拦截器
  10. 敏感性、特异性、假阳性、假阴性(sensitivity and specificity)