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. display:inline和display:inline-block的区别

    先来一张图: 测试代码: <!DOCTYPE html> <html> <head> <style> #bb { overflow: hidden; b ...

  2. .NET 笔试分享

    最近一直在面试,每次面试前也不怎么准备,虽说碰到的题大部分都很简单的,但是在现场答题的时候由于自己紧张脑子就空了,一些题答的不是很好,所以只有每次回来的时候才能好好想想怎么答: 题大部分还是挺简单的, ...

  3. IOS 错误集合以及解决办法(持续整理中)

    1 . 如下错误: app:resource fork, Finder information, or similar detritus not al site:forums.developer.ap ...

  4. DTO学习系列之AutoMapper(二)

    本篇目录: Flattening-复杂到简单 Projection-简单到复杂 Configuration Validation-配置验证 Lists and Array-集合和数组 Nested m ...

  5. 解决mdi窗体闪烁的问题

    /// 解决mdi窗体闪烁的问题 /// </summary> protected override CreateParams CreateParams { get { CreatePar ...

  6. hdu 2199

    Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its sol ...

  7. QT5-控件-QComboBox

    #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QComboBox> cl ...

  8. 洛谷 P1896 互不侵犯King

    P1896 [SCOI2005]互不侵犯King 题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共 ...

  9. CODEVS 3137 栈练习1

    3137 栈练习1 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只 ...

  10. PowerShell_零基础自学课程_3_如何利用Powershell ISE调试PS脚本

    微软在推出PS的同时,没有忘记其一贯的作风,什么东东都弄一个IDE环境,这不微软没有忘记给PS也来一个IDE的环境, 通过这个IDE环境,可以建立psl文件,可以调试psl文件. 1.IDE界面 我们 ...