[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 you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension events, and show how they can be used to manipulate the request or response in-flight.
const Hapi = require( 'hapi' )
const Boom = require( 'boom' )
const server = new Hapi.Server()
server.connection( { port: 8000 } ) server.ext( 'onRequest', ( request, reply ) => {
request.setUrl( '/' )
request.setMethod( 'GET' )
// need to call continue, the same as Express's next()
reply.continue()
} ) /**
* After the request has gone through the router, it needs to be authenticated. OnPreAuth runs before authentication, and onPostAuth runs after.
*/
server.ext( 'onRequest', ( request, reply ) => {
console.log( 'onRequest' )
reply.continue()
} ) /**
* After the request has gone through the router, it needs to be authenticated. OnPreAuth runs before authentication, and onPostAuth runs after.
*/
server.ext( 'onPreAuth', ( request, reply ) => {
console.log( 'onPreAuth' )
reply.continue()
} ) server.ext( 'onPostAuth', ( request, reply ) => {
console.log( 'onPostAuth' )
reply.continue()
} ) /**
* Validation is handled next. OnPreHandler runs, then, the route itself.
*/
server.ext( 'onPreHandler', ( request, reply ) => {
console.log( 'onPreHandler' )
reply.continue()
} ) /**
* OnPostHandler runs after the handler.
*/
server.ext( 'onPostHandler', ( request, reply ) => {
console.log( 'onPostHandler' )
reply.continue()
} ) /**
* Then, the response payload is validated on pre-response runs, and the response is sent to the client.
*/
server.ext( 'onPreResponse', ( request, reply ) => {
console.log( 'onPreResponse' )
reply.continue()
} ) server.route( {
method: 'GET',
path: '/',
handler: function ( request, reply ) {
console.log( 'handler' )
reply( 'hello world' )
}
} ) server.start( () => {
} )
What are these extensions good for? Each extension can be used to manipulate the request or response at any point in the lifecycle. For example, in onRequest, I can force all requests to be interpreted as GETs to the route path.
To do so, I'll use request.seturl, pass in the string/, and pass the string GET to request.setmethod. Now, when I make a post request to /foo, it still hits my defined route.
[Hapi.js] Extending the request with lifecycle events的更多相关文章
- [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] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [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] View engines
View engines, or template engines, allow you to maintain a clean separation between your presentatio ...
- [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] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [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] Replying to Requests
hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...
- [Hapi.js] Route parameters
Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path param ...
随机推荐
- mp4文件格式之fragment mp4
目前网络上对mp4文件格式的总结已经相当多了,我就不在这里抄别人总结的东西了,想入门的话百度一下会有许多这方面的文章.这里介绍一下其他文章中很少涉及的一种mp4文件结构,即fragment mp4,也 ...
- C# 导出Excel 多个Sheet
以下代码中最关键的代码是 Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, ...
- 未能加载文件或程序集“System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”
最近用vs2012发布程序,然后将更新后的程序文件部署到服务器上,由于服务器上本来有此系统,所以只更新了修改的文件 . 进行系统登录时提示:未能加载文件或程序集“System.Web.Extensio ...
- 网站全局js代码
这几天开始看公司的一套系统,整理的网站全局js代码 /*文件名:base.js功能说明:全站通用的全局变量及公用方法创建日期:2010-09-26*///引入jquery库文件document.wri ...
- Silverlight 图表下载到Excel文件中
一.Silverlight xaml.cs文件按钮触发方法 1.//下载图表 private void btnDown_Click(object sender, RoutedEventA ...
- iOS开发-清理缓存功能的实现
移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...
- 清空文本框SetDlgItemText(IDC_TXTXT,NULL);
清空文本框 SetDlgItemText(IDC_TXTXT,NULL);
- <c:if>标签
<c:if>的用途就和我们一般在程序中用的if一样. 语法 语法1:没有本体内容(body) <c:if test="testCondition" var=&qu ...
- 写下你的第一个Django应用,第三部分
这篇指南开始于指南2结束的地方.我们将继续web投票应用和集中注意力在创建公共接口——“view” 理念 一个视图在你的Django应用中一个web页面的“品种”和它通常作为一个特定的函数以及有一个特 ...
- Android DrawerLayout 点击事情穿透
今天使用DrawerLayout做网易4.4版本侧边栏发现点击DrawerLayout空白部分,下面部分content会获得点击事件.解决方法是在<!-- The navigation draw ...