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

 

posted @   Zhentiw  阅读(626)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示