Expresss 路由
来源:互联网 发布:苹果6有信号没网络 编辑:程序博客网 时间:2024/06/05 18:46
前言
Express提供了三种路由方案,path
, Router
, route
。中间件哲学,每个负责具体业务实现,都与中间件行为相似。同时提供了三种路由方式,各有优劣。
路由特别说明
使用app.use()
方式,所有以path
参数开头的路径,都会使用该中间件,具体的HTTP verb
则为完全匹配方式。
// will match paths starting with /abcdapp.use('/abcd', function (req, res, next) { next();})
每次请求,都会先做路径匹配,判定需要调用哪些函数,然后按序调用。app.param()
则在序列开始前调用,用以路径参数合法化检查。
app.param('identity', function(req, res, next, identity) { if (/^[A-Za-z]+$/.test(identity)) { next(); } else { res.status(404).end(); }});app.get('/author/:identity', function(req, res) { res.send('hello world!');});
如果路径全为字母,则向下调用,否则不进入调用序列。测试如下:
describe('express param', function () { it('param identity pass', function (done) { request .get('/author/angular') .expect(200) .end(done); }); it('param identity pending', function (done) { request .get('/author/angular12') .expect(404) .end(done); });});
文档貌似示例可以这样写,但在本机上测试未成功。
app.param('identity', /^[A-Za-z]+$/);
不建议在这个地方过于深究,大概了解作用即可。
路由定义
简单情况之下,可以直接使用path
定义路由。
app.use('/', function(req, res, next) { next();});
所有/
开头的路径都会引用处理,非常方便使用。假设nodejs
实现restful API
,需要定义resource
。假设blog
为资源,使用path
方式实现如下
app.get('/blog/', function(req, res) { res.send('blog list');});app.get('/blog/:identity', function(req, res) { res.send('blog content');});app.post('/blog/:identity', function(req, res) { res.send('blog update');});app.put('/blog/', function(req, res) { res.send('blog create');});app.delete('/blog/:identity', function(req, res) { res.send('blog delete');});
使用Router
方式如下实现:
var blog = express.Router();blog.get('/', function(req, res) { res.send('blog list');});blog.get('/:identity', function(req, res) { res.send('blog content');});blog.post('/:identity', function(req, res) { res.send('blog update');});blog.put('/', function(req, res) { res.send('blog create');});blog.delete('/:identity', function(req, res) { res.send('blog delete');});app.use('/blog', blog);
从代码上看出,使用Router
并不会节省代码,而且结构高度相似。但是单个url
更短,构建restful API
时,更加直观。私以为使用Router
方式效率更高,所以做了一个简单测试。定义了四个resource
,author
,blog
, comment
, draft
,分别使用path
,Router
来实现,大概实现代码更上面所述高度相似,不在赘述。
app.use(function(req, res, next) { console.log('routerA start time stamp %d', Date.now()); next();});
在最上方使用此方式,用以记录参照时间。在请求URL函数中,输出当前时间戳,用以比较时间消耗。输出结果如下(routerA为采用Router方式,routerB为采用path方式):
/usr/local/bin/node routerA.jsrouterA start time stamp 1413270521788routerA end time stamp 1413270521800/usr/local/bin/node routerB.jsrouterB start time stamp 1413270521826routerB end time stamp 1413270521836
测试结果path
比Router
方式快2ms,差距不大,但与预期结果相反,可能是测试方式存在错误,并不表示公正。速度并不是考虑因素,而是应该考虑具体的借口方式。如果是restful API
,建议采用Router
方式,梳理起来更加轻松。再看一下app.route()
使用方式。
app.route('/route') .get(function(req, res) { res.send('route get shuffle'); }) .post(function(req, res) { res.send('route post shuffle') });
其返回对象可以使用HTTP verbs
以及其他中间件处理函数,唯一需要注意的点,get
等方法只接受一个类中间件函数作为参数,不能传入值做URL
处理。
app.route('/route') .get('/event', function(req, res) { res.send('route get shuffle'); })
这种处理方式错误。
总结
路由定义,path
方式万能,适合所有情况。Router
方式通常为路径前缀统一的情况使用,如定义resource
。route
方式通常为路径统一的情况使用,通常应该见于面向URL的API定义。总结来说,只是为了接口定义在代码层级的梳理,让接口定义方式更加优雅。
- Expresss 路由
- ng Expresss POST
- 使用node+expresss新建node项目
- Expresss实现商品列表查询上
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- 路由
- MYSQL中的普通索引,主健,唯一,全文索引区别
- 教你如何做好微信营销说到微信营销
- DroidWorks- 在ActionBar中嵌入TabBar
- java 23种常用设计模式之外观模式(Facade)
- gsoap使用心得! (win32)
- Expresss 路由
- Android——手机sd卡的大小查看
- onclick="return XX()"解析
- hdfs mount为linux本地文件系统
- Android笔记----Android的数据存储和IO操作
- Android——查询通话记录和删除记录
- 软件测试基础视频
- linux hadoop mount 加载HDFS到本地文件系统
- Android 利用javah.exe 生成JNI头文件 时的注意事项