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的更多相关文章

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

  2. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  3. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  4. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

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

  6. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  7. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

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

  9. [Hapi.js] Route parameters

    Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path param ...

随机推荐

  1. storm启动流程

    email:chenguibin2004@126.com storm: 是一个分布式的实时流式计算框架,具有低延迟.高可用.分布式.可扩展.数据不丢失的特点, storm包含四个核心组件: Nimbu ...

  2. DEV PivotGridControl 全选行或列

    foreach (string item in fieldProductName.FilterValues.Values) { pivotGridControl.Cells.SetSelectionB ...

  3. <!DOCTYPE html>的问题

    <!DOCTYPE> 声明必须位于 HTML5 文档中的第一行,也就是位于 <html> 标签之前.该标签告知浏览器文档所使用的 HTML 规范. doctype 声明不属于 ...

  4. 17 java 存在的问题(转)

    java的问题: 1.性能:java的内存管理似乎比较自动化,但其实性能不是特别好.尤其是new对象的时候没有节制.在java中,有些对象构造成本很低,有些 很高.特别在UI编程的时候,大多数的UI对 ...

  5. JS中的== 、===的用法和区别。

    JS中的== .===的用法和区别.[转] == 和 != 比较若类型不同,先偿试转换类型,再作值比较,最后返回值比较结果 . 而  === 和 !== 只有在相同类型下,才会比较其值 ======= ...

  6. jquery 实现 隐藏交替同时记住以前隐藏的样式

    /* * control menu show or hide(expand and collapse) */ var status = []; function menuOperation() { $ ...

  7. C语言解析日志,存储数据到伯克利DB

    编译命令 gcc -o dbwriter dbwriter.c -ldb dbwriter.c #include <assert.h> #include <stdlib.h> ...

  8. IC封装图片认识(二):TO封装图

    TO-18 TO-220 TO-247 TO-264 TO-52 TO-71 TO-72 TO-78 TO-8 TO-92 TO-93 TO-99 TO-263&268 FTO-220 ITO ...

  9. 操作系统对的IIS版本

    IIS版本 Windows版本 备注 IIS 1.0 Windows NT 3.51 Service Pack 3   IIS 2.0 Windows NT 4.0   IIS 3.0 Windows ...

  10. linux操作系下RAR的使用

    ============zip文件的操作================================== zip -r data.zip data 解释:将data文件夹压缩成了data.zip格 ...