[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 ...
随机推荐
- 消息摘要算法-HMAC算法
一.简述 mac(Message Authentication Code.消息认证码算法)是含有密钥散列函数算法.兼容了MD和SHA算法的特性,并在此基础上加上了密钥.因此MAC算法也常常被称作HMA ...
- Android 编程下 Activity 的创建和应用退出时的销毁
为了确保对应用中 Activity 的创建和销毁状态进行控制,所以就需要一个全局的变量来记录和销毁这些 Activity.这里的大概思路是写一个类继承 Application,并使获取该 Applic ...
- C#获取时间戳的方法
获取时间戳的方法 /// <summary> /// 获取时间戳 /// </summary> /// <param name= ...
- JQuery.ajax一解
关于JQuery.ajax方法,好处也不用多说了,主要是想记下ajax中的一些参数: url:请求的目标地址,为一个字符串,格式为:http://localhost:端口号/User/方法名.eg:现 ...
- C#中public、private、protected、internal、protected internal (转载)
在C#语言中,共有五种访问修饰符:public.private.protected.internal.protected internal.作用范围如下表:访问修饰符 说明public 公有访问.不受 ...
- WebApi2官网学习记录---Media Formatters
Web API内建支持XML.JSON.BSON.和form-urlencoded的MiME type. 创建的自定义MIME类型要继承一下类中的一个: MediaTypeFormatter 这个类使 ...
- 全世界最详细的图形化VMware中linux环境下oracle安装(一)【weber出品必属精品】
安装流程:前期准备工作--->安装ORACLE软件--->安装升级补丁--->安装odbc创建数据库--->安装监听器--->安装EM <前期准备工作> 安装 ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- 关于HTML的使用。
一丶标签问题 对于初学者来说,无疑用得最多的标签就是div和span了,当然就算只用这2个标签也能写出一个好看的页面,但是W3C为什么会给我们这么多标签来选择呢? 从浏览器的渲染来讲,标签的语义话,更 ...
- Form 表单常用正则验证 (收藏)
1.^\d+$ //匹配非负整数(正整数 + 0) 2.^[0-9]*[1-9][0-9]*$ //匹配正整数 3.^((-\d+)|(0+))$ //匹配非正整数(负整数 + 0) 4.^-[0-9 ...