[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.
[Hapi.js] Route parameters的更多相关文章
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [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 ...
- [Hapi.js] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [Hapi.js] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [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 ...
- [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 ...
- [Hapi.js] Serving static files
hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...
随机推荐
- 《javascript设计模式》读书笔记四(单例模式)
1.单利模式简单介绍 在<设计模式>中单利模式是一种比較简单的模式,定义例如以下: 确保某一个类仅仅有一个实例,并且自行实例化并向整个系统提供这个实例. 在javascript中则将代码组 ...
- [小技巧] 把虚拟机中的Linux系统安装到U盘中
出于各种需求,很多用户可能经常会在Windows系统中安装虚拟机,然后在虚拟机中安装Linux系统.使用虚拟机的优点是可以同时使用多个系统,而缺点也是显然的,也就是程序运行效率较差. 而实际上,L ...
- C#工厂模式代码实例
此处示例为一个简易计算器工厂模式的实现. 创建类库,名为CalcLib,我把计算功能全部放在这个类库中. 首先,创建一个抽象的计算器算法父类,如下: /// <summary> /// 计 ...
- 什么是系统平均负载(Load average)
一.什么是系统平均负载(Load average)? 在Linux系统中,uptime.w.top等命令都会有系统平均负载load average的输出,那么什么是系统平均负载呢? Load Aver ...
- Gstreamer 中的playback插件
1. PLAYBACK插件基本介绍 在早期的版本中同时存在playbin和playbin2,但是在最新的版本中,playbin2已经稳定,取代了playbin, playbin不再进行维护.下面是官网 ...
- IIS注册asp.net 4.0
如果你是先装的VS后添加的IIS功能,那么你需要在ISS中注册NET Framework: 32位的Windows:------------------------------------------ ...
- sqlite创建数据库问题
1.<Sqlite权威指南>上说是这么创建数据库的: sqlite3 test.db 但是我写了这条语句之后出现了下面的情况(注:安装Sqlite过程见 ...) 我的sqlite3放在 ...
- DropDownList绑定数据
DDLName.DataSource = myRd;DDLName.DataTextField = "name";//要绑定的字段DDLName.DataValueField = ...
- 如何在Silverlight应用程序中获取ASP.NET页面参数
asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
转载:http://blog.sina.com.cn/s/blog_629e606f01014d4b.html ComboBox最经常使用的事件就是SelectedIndexChanged.但在将Co ...