[Hapi.js] Route parameters
Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path parameters in hapi's router. We'll also touch on how the router uses specificity to order routes internally.
Router Param:
http://localhost:8000/ztw
// Will match any string after /
server.route( {
method: 'GET',
path: '/{name}',
handler: function ( request, reply ) {
reply( "hello " + request.params.name + "!" ); // hello ztw
}
} );
http://localhost:8000/user/ztw:
server.route( {
method: 'GET',
path: '/user/{name}',
handler: function ( request, reply ) {
reply( "hello " + request.params.name + "!" );
}
} );
Optional parameters
server.route( {
method: 'GET',
path: '/user/{name?}',
handler: function ( request, reply ) {
var name = request.params.name ? request.params.name : "AAAA";
reply( "hello " + name + "!" );
}
} );
http://localhost:8000/user/ztw:
// hello ztw!
http://localhost:8000/user/:
// hello AAAA!
server.route( {
method: 'GET',
path: '/user/{name}/address',
handler: function ( request, reply ) {
reply( request.params.name + "'s address: Finland" );
}
} );
http://localhost:8000/user/ztw/address:
// ztw's address: Finland
Multi-segment parameters
server.route({
method: 'GET',
path: '/hello/{user*2}',
handler: function (request, reply) {
const userParts = request.params.user.split('/');
reply('Hello ' + encodeURIComponent(userParts[0]) + ' ' + encodeURIComponent(userParts[1]) + '!');
}
});
http://localhost:8000/hello/ztw/twz:
// Hello ztw twz!
If pass the third params, it will report error.
server.route({
method: 'GET',
path: '/files/{file*}',
handler: function(request, reply){
reply(request.params);
}
})
http://localhost:8000/files/sky/night/aurora/100.jpg:
// {"file":"sky/night/aurora/100.jpg"}
server.route({
method: 'GET',
path: '/files/{file}.jpg',
handler: function(request, reply){
reply(request.params);
}
})
http://localhost:8000/files/100.jpg:
// {"file":"100"}
The last thing I'd like to cover is path specificity. Hapi's router evaluates routes in order from most specific to least specific, which means that the order routes are created does not affect the order they are evaluated.
server.route({
method: 'GET',
path: '/files/{file*}',
handler: function(request, reply){
reply(request.params);
}
})
server.route({
method: 'GET',
path: '/files/{file}.jpg',
handler: function(request, reply){
reply(request.params);
}
})
If I give the url:
http://localhost:8000/files/100.jpg, it will match the second router instead of the first router.
But if I gave http://localhost:8000/files/b/100.jpg
// {"file":"b/100.jpg"}
It will match the first router.
[Hapi.js] Route parameters的更多相关文章
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [Hapi.js] Logging with good and good-console
hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...
- [Hapi.js] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [Hapi.js] Friendly error pages with extension events
hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...
- [Hapi.js] Extending the request with lifecycle events
Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...
- [Hapi.js] Serving static files
hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...
随机推荐
- Swift 2.0 封装图片折叠效果
文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...
- ulimit 参数介绍
Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数 可以用ulimit -a 来显示当前的各种用户进程限制.下面我把某linux用户的最 ...
- 用CSS3写的钟表
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 豆瓣移动端风格的css命名方法与学习
在CSS取名相对于刚入门来说是最头疼的事情,往往取一个可读性的名字相对以后的代码风格还是很重要的. 在配合团队方面一个好的类名可以帮助同事来理解,增加团队之间的效率有着很大的意义. 豆瓣的前段相对其他 ...
- Oracle11g R2学习系列 之九 PL/SQL语言
这是个重头戏,如果精通了PL/SQL,毫不夸张的说明精通了Oracle了.PL/SQL由以下三个部分组成(Definition,Manipulation,Control): DDL:数据定义语言,Cr ...
- 【JAVA编码专题】 JAVA字符编码系列三:Java应用中的编码问题
这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记录下来以便日后参考. 为了构成一个完整的对文字编码的认识和深入把握,以便处理在Java开发过程中遇到的各种问 ...
- Gora官方范例
参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...
- Python(四):数字连珠2
对上次的代码作了一些修改.在码的过程中发现,最核心的部分是在横向.竖向和两个对角方向上找到5个以上相同的数字. 自己的思路是将x行y列所在的x行.y列,以及以此为交叉点的两点对角线上的数字,转化成字符 ...
- 微软企业库的Cache
微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSamp ...
- 拼接xml时出现内存溢出解决办法
/// designed by Alex /// 解决内存溢出问题,不可使用迭代器和foreach /// 循环遍历List, 别生成xml,一边释放 /// 反向遍历,相当于出栈,避免索引顺序乱的问 ...