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. 使用Win32::OLE操作Excel——Excel对象模型

    像VBA操作Excel一样,Win32::OLE模块也是通过对象操作来控制Excel. 如果想自动化操作和控制Excel应用程序,则必须要与Excel对象模型所提供的对象进行交互.理解和熟悉Excel ...

  2. oracle监听服务开启

    输入命令netca即可开启oracle的监听服务 弹出对话框 选择监听服务配置,单击下一步 选择增加监听,单击下一步 监听的名字,默认即可,下一步 监听链接的协议,默认TCP协议即可,下一步 监听默认 ...

  3. Base64加密解密

    /// <summary> /// 实现Base64加密解密 /// </summary> public sealed class Base64 { /// <summa ...

  4. android 隔几秒再执行

    今天做项目,需要前面的方法执行完等待2秒在关闭当前页面.之前使用的是Thread.sleep(2000)发现根本没有作用.经过多次尝试,发现需要使用以下方法才能实现: new Thread(){ pu ...

  5. IP地址基础和子网规划之其一

    IP地址的介绍:在TCP/IP环境中,各种各样的终端.工作站能同服务器.其他工作站无缝连接,是因为每一网络节点都使用了全网范围内能够唯一标识节点的IP地址.每个网络有一个全网唯一的网络号,在该网络中各 ...

  6. oracle 自动添加序号列 排序

    select      HSL.sortno,                    HSL.B,                    HSL.A,                    row_n ...

  7. mysql查询分组归类函数-group_concat,通常与group_by一起使用

    select a.`name`,group_concat(b.name SEPARATOR'.') as persons from `group` as a,`person` as b,`person ...

  8. 用Java来比较JavaScript的一些特性

    由于是从java做到JavaScript,所以对强弱语言类型,还是比较敏感的.JavaScript是弱语言,只严格区分数据和指令.简单描述下自己对两者之间的一些区别吧. 1.JavaScript变量的 ...

  9. 初识cross apply & outer apply

    1. 2. 3.参考地址: http://blog.csdn.net/htl258/article/details/4537421

  10. 转载 50种方法优化SQL Server数据库查询

    原文地址 http://www.cnblogs.com/zhycyq/articles/2636748.html 50种方法优化SQL Server数据库查询 查询速度慢的原因很多,常见如下几种: 1 ...