[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 ...
随机推荐
- 在 VS2008 下操作 Excel 的方法总结
这些天做个软件,需要读取 Excel 并导入到数据库中,所以研究了一下在 VC 下操作 Excel 的方法,这里做个总结,以作备忘. 一.最常用的 OLE 自动化方式 这个方式应该说是功能最全的方 ...
- Java_.jar .war .ear 详解
.jar 全称: java archive: 包含: class.properties文件,是文件封装的最小单元: ...
- 支持多QQ登录的软件
支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件
- (转)js正则表达式之中文验证
今天做表单提交的输入框条件验证,验证是否包含中文:网上搜了一圈基于js正则表达式的验证基本不好用,而且大多都是出自一两篇原文的转帖!到底什么才是拿来主义呢.根据搜索结果,本文取精华,告诉大家一个好用的 ...
- 调用具体webservice方法时时报错误:请求因 HTTP 状态 503 失败: Service Temporarily Unavailable
添加web引用会在相应项目的app.cofig文件中产生如下代码: <sectionGroup name="applicationSettings" type="S ...
- IOS网络开发实战(一)
1 局域网群聊软件 1.1 问题 UDP协议将独立的数据包从一台计算机传输到另外一台计算机,但是并不保证接受方能够接收到该数据包,也不保证接收方所接收到的数据和发送方所发送的数据在内容和顺序上是完 ...
- 空值排序(oracle/sqlserver)
oracle认为 null 最大. 升序排列,默认情况下,null值排后面. 降序排序,默认情况下,null值排前面. 改变空值办法: (1)用nvl函数或decode函数将null转换为一特定值 替 ...
- 一个支持实时预览的在线 Markdown 编辑器 - Markdoc
最近组内需要为一些项目和系统写文档,发表在公司内的文档平台上,这个平台并不支持markdown,所以打算做一个在线markdown编辑器,支持实时预览,并且可以很容易的迁移发表到公司文档平台上,所以就 ...
- 最大公约数(gcd):Euclid算法证明
1个常识: 如果 a≥b 并且 b≤a,那么 a=b. 2个前提: 1)只在非负整数范围内讨论两个数 m 和 n 的最大公约数,即 m, n ∈ N. 2)0可以被任何数整除,但是0不能整除任何数,即 ...
- Hdu1094
#include <stdio.h> int main() { ; while(scanf("%d",&n)!=EOF){ ;i<n;i++){ scan ...