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的更多相关文章

  1. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  2. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  3. [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 ...

  4. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  5. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  6. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  7. [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 ...

  8. [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 ...

  9. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

随机推荐

  1. web负载均衡整理

    参考:http://www.cnblogs.com/lovingprince/archive/2008/11/13/2166350.html http://www.cnblogs.com/loving ...

  2. http 压缩

    HTTP压缩是在Web服务器 和浏览器间传输压缩文本内容的方法.HTTP压缩采用通用的压缩算法如gzip等压缩HTML.JavaScript或 CSS文件.压缩的最大好处就是降低了网络传输的数据量,从 ...

  3. linux打开80端口及80端口占用解决办法

    linux打开80端口天客户那边有台服务器同一个局域网中都无法访问,排除lamp环境问题,发现时服务器中的防火墙没有开启80端口. 代码如下 复制代码vi /etc/sysconfig/iptable ...

  4. hdu2588 gcd 欧拉函数

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 织梦dedecms返回上一级链接代码

    如题:织梦dede手机页面,如果我进入了下一级页面,想回上一级,<a href="xx">该用什么标签? 用JS实现,代码如下 <a href="jav ...

  6. talos项目记录

    1. 跑schedule : php src/cli-schedule/cli.php -a sales.coupon.offer-coupons  所用方法在schedule里

  7. hadoop集群之HDFS和YARN启动和停止命令

    假如我们只有3台linux虚拟机,主机名分别为hadoop01.hadoop02和hadoop03,在这3台机器上,hadoop集群的部署情况如下: hadoop01:1个namenode,1个dat ...

  8. 一种针对虚拟机的应用软件License认证方法

    由于虚拟机的硬件信息可以随意修改,使得虚拟机可能具有相同的硬件信息,在传统的应用软件License认证方式中会导致License认证漏洞.本专利提供了一种有效的解决方法. 文/王宏财 目 前,云计算的 ...

  9. 分布式文件系统glusterfs安装步骤

    我的系统是 RHEL5 , 可能环境不一样, 需要安装的第三方依赖不一样啊, 反正大家在安装的过程中缺少什么就去安装什么, 一般都会有提示的. 下载   glusterfs-3.2.0.tar.gz  ...

  10. Qt HTTP请求同步调用

    在Qt中,进行HTTP就行现在官方提倡使用QNetworkAccessManager,其和QNetworkRequest和QNetworkReply配合使用,来完成,其是只支持异步的操作.最近使用QM ...