[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 ...
随机推荐
- 注册AxtiveX控件
Win8.1或者Win7下 首先在“管理员的身份”运行cmd 然后输入:regsvr32 D:\***\*.ocx
- 原生 javascript 学习之 js变量
1.变量的命名 方法的命名(驼峰命名法) 全部小写 : 单词与单词之间全部下划线 (my_namespace) 大小写混合 : 第一个单词首字母小写其他单词首字母大写. 规则 首字符 英文字母或下划线 ...
- CSS 实现三角形、梯形、等腰梯形
三角形 ; width: 0px; border-width: 0px 30px 45px 145px; border-style: none solid solid; border-color: t ...
- kmp代码实现
/* kmp彻底理解 next 数组 :用来指导S[i]串 T[j]串 对应字符失配 指导 i 不回溯,即j应该走多少个位置 next[j]:j位置前一个元素 需要 计算某个字符对应的next值,就是 ...
- performSelector的方法
在此我对performSelector系列方法进行了总结 1. - (id)performSelector:(SEL)aSelector; - (id)performSelector:(SEL)aSe ...
- java必备基础知识(一)
学习的一点建议: 每一门语言的学习都要从基础知识开始,学习是一个过程,"万丈高楼平地起",没有一个好的地基,想必再豪华的高楼大厦终究有一天会倒塌.因此,我们学习知识也要打牢根基,厚 ...
- hdu 2191多重背包
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- log4j配置文件及nutch中的日志配置
使用slf4j作为日志系统时,由于slf4j只是一个接口,它需要一个具体实现来执行. 具体参考http://blog.csdn.net/jediael_lu/article/details/43854 ...
- Gulp:基于流的自动化构建工具
前言 先说说为什么会使用gulp. 当你沉醉于撸代码之时,是否想过正规的前端代码需要走哪些流程,复杂的不说了,有几点想必你也思考过,比如: 1.代码的压缩合并.图片压缩怎么搞: 2.代码校验,是否规范 ...
- 如何在异步请求时设置RequestHeader
一.为何要用到setRequestHeader 通常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTTP协议的头文件,告诉服务器客户端要下载什么信息以及相关的参数.而 XMLHTT ...