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. NSLog用法,打印日志

    要输出的格式化占位:   %@ 对象 %d, %i 整数 %u   无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e   浮点/双字 (科 ...

  2. c++11 : range-based for loop

    0. 形式 for ( declaration : expression ) statement 0.1 根据标准将会扩展成这样的形式: 1   { 2     auto&& __ra ...

  3. Asp.net中实现同一用户名同时登陆,注销先前用户(转)

    Web 项目中经常遇到的问题就是同一用户名多次登陆的问题,相应的解决办法也很多,总结起来不外乎这几种解决办法:将登陆后的用户名放到数据库表中:登陆后的用 户名放到Session中:登陆后的用户名放到A ...

  4. c语言构建动态数组

    #include <stdio.h> #include <stdlib.h> int main(void) { int len; int * arr; printf(" ...

  5. POJ 1002 - 487-3279 STL

    先把不是标准格式的字符串变成标准格式再输出出现两次以上的标准串和出现的次数不然输出 "No duplicates." #include <iostream> #incl ...

  6. jquery dataTable 入门

    step1:切记要先引入jquery <link rel="stylesheet" type="text/css" href="C:\Users ...

  7. 关于jquery-easyUI中主键属性data-options的了解

    data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中,例如: <div class=& ...

  8. Java根据字节数据判断文件类型

    通常,在WEB系统中,上传文件时都需要做文件的类型校验,大致有如下几种方法: 1. 通过后缀名,如exe,jpg,bmp,rar,zip等等. 2. 通过读取文件,获取文件的Content-type来 ...

  9. Python学习笔记九-文件读写

    1,读取文件: f=open('目录','读写模式',encoding='gbk,error='egiong') 后三项可以不写但是默认是' r'读模式:open函数打开的文件对象会自动加上read( ...

  10. docker --- 初识

    Docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机).ba ...