Hapi通过插件传递回复对象

时间:2017-06-10 16:14:40

标签: node.js hapijs

我试图通过hapi中的插件实现一个回复助手。该插件将requestreply对象传递给助手类。但我认为调用reply.continue()将控制权返回给hapi会更改回复对象。我的插件

const replyPlugin = {
  register: function registerReplyHelper(server, options, next) {
    server.ext('onRequest', (request, reply) => {
      replyHelper.setRequest(request);
      replyHelper.setReply(reply);
      reply.continue();
    });

    next();
  }
};

然后我从帮助程序类调用reply()

this.reply(data);

虽然执行了这一行,但请求并未以响应结束,而是被卡住了。我已经尝试将setReply(reply)语句移动到处理程序并且它有效,但这不是我希望它实现的方式。有没有办法让前一个实现工作?

更新setRequestsetReply方法以及类的构造函数如下 -

  constructor() {
    this.request;
    this.reply;
    this.id;
  }

  /**
   * Sets the [request] property of object
   * @param {object} request
   */
  setRequest(request) {
    this.request = request;
    if (request.params.id) {
      this.id = request.params.id;
    } else {
      this.id = null;
    }
  }

  /**
   * Sets the [reply] property of object
   * @param {object} reply
   */
  setReply(reply) {
    this.reply = reply;
  }

0 个答案:

没有答案