node.js - 理解hapi js的生命周期

标签 node.js hapijs hapi.js

  register: async (server, options) => {

        server.ext('onRequest', (request, h) => {
            if (request.params) {
                log.verbose(`onRequest:${request.method.toUpperCase()}:${request.path}/${request.params}`)
            } else {
                log.verbose(`onRequest:${request.method.toUpperCase()}:${request.path}`)
            }

            if (Object.getOwnPropertyNames(request.query).length) {
                log.verbose(`onRequest:queryParameters: ${JSON.stringify(request.query)}`)
            }

            if (request.headers && request.headers['x-access-token']) {
                log.verbose(`onRequest:heders:x-access-token ${JSON.stringify(request.headers['x-access-token'])}`)
            }

            return h.continue
        })

        server.ext('onPreAuth', (request, h) => {
            log.verbose('onPreAuth')
            return h.continue
        })

        server.ext('onCredentials', (request, h) => {
            log.verbose('onCredentials')
            return h.continue
        })

        server.ext('onPostAuth', (request, h, error) => {
            if (request.payload) {
                log.verbose(`onPostAuth:bodyPayload: ${JSON.stringify(request.payload)}`)
            }
            return h.continue
        })

        server.ext('onPreHandler', (request, h) => {
            log.verbose('onPreHandler')
            return h.continue
        })

        server.ext('onPostHandler', (request, h) => {
            log.verbose('onPostHandler')
            return h.continue
        })

        server.ext('onPreResponse', (request, h) => {
            if (request && request.response && request.response.source) {
                try {
                    log.verbose(`onPreResponse:${JSON.stringify(request.response.source)}`)
                } catch (err) {
                    log.warn(err)
                    log.verbose(h.request.response.source.toString())
                }
            }
            return h.continue
        })
    }

这些是 Hapi 生命周期:
  • OnRequest:请求到达服务器时调用。
  • OnPreAuth:当请求到达路由的认证部分时调用。
  • OnPostAuth:当请求从路由的 auth 部分发出时调用。
  • OnPreHandler:当请求到达路由的处理程序部分时调用。
  • OnPostHandler:当请求从路由的 Handler 部分发出时调用。
  • OnPreResponse:返回响应时调用。

    7 onCredentials:是 Hapi v17 的新内容。

    无法理解 onCredentials 的用途。
    另外,如果我理解错误的 hapi 生命周期,请纠正我。
  • 最佳答案

    你的理解是正确的。 onCredentials 添加到 OnPreAuth 和 OnPostAuth 之间,这里有一些关于它的信息。

    A new onCredentials extension point and the ability to change the request credentials before authorization is validated.



    来源:https://github.com/hapijs/hapi/issues/3658

    这里还有一点

    onCredentials: a new Request Extension Point Each request served with hapi follows a predefined path: the request lifecycle. Depending on whether you need authentication or validation, the framework skips individual lifecycle points.

    There’s a new extension point in hapi v17: onCredentials. This extension point locates after onPreAuth and before onPostAuth. In onPreAuth, hapi authenticates the request and identifies the user. The authorization is part of onPostAuth, like checking the request scope to verify that the request has access rights.

    In onCredentials, you can customize the credentials before the request authorization.



    来源:https://futurestud.io/tutorials/hapi-v17-upgrade-guide-your-move-to-async-await

    这意味着您可以修改凭据对象。这是简单的示例代码。假设您想根据用户信息在身份验证后更新凭据范围。
    server.ext('onCredentials', (request, h) => {
    
        request.auth.credentials.scope = 'customadmin';
        return h.continue;
    });
    

    Hapi.js Scope Explanation

    关于node.js - 理解hapi js的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50909992/

    相关文章:

    json - 使用 Joi 验证 JSON 查询字符串作为查询参数

    node.js - 在 swagger ui 中隐藏一些 api

    javascript - 在 Hapi.js 中提供静态 JavaScript 文件不起作用

    node.js - 从通过 php gear 托管的客户端连接到 socket.io? - 开档

    node.js - 在 sequelize.js 中使用包含时如何将所有列放在一行中?

    mysql - Cloud9 IDE : Cannot Connect to MySQL via Node

    angularjs - Angular $ http.delete + hapijs使用swagger + grails

    javascript - Node.js 和 hapi : fetching data from a database synchronously

    javascript - nodejs websocket 服务器中的内存泄漏

    javascript - 使用 CryptoJS 将解密格式解析为具有大部分填充的字符串时出现问题