是否可以在hapi中返回服务器方法的文件

时间:2014-10-08 16:33:17

标签: node.js hapijs

我试图将reply.filehttp://hapijs.com/api#replyfilepath-options)用于server.methodhttp://hapijs.com/api#servermethodname-fn-options),但它说我的next函数没有&file #39; t Debug: hapi, internal, implementation, error TypeError: Uncaught error: Object function (err, result) { methodNext(err, result, null, { msec: timer.elapsed(), error: err }); } has no method 'file' at /Users/user/Work/Dev/export.js:37:12 at ChildProcess.<anonymous> (/Users/user/Work/Dev/node_modules/webshot/lib/webshot.js:221:9) at ChildProcess.emit (events.js:98:17) at Process.ChildProcess._handle.onexit (child_process.js:810:12)

server.method('getExport', function(reqParams, queryParams, hostUri, next) {

  // ...
  next.file(filePath, {
    // Download as an attachment
    mode: 'attachment'
  });

});


Instance.server.route({
  method: 'GET',
  path: '/export',
  config: {
    handler: function(req, reply) {
      var uri = req.server.info.uri;

      server.methods.getExport(
        req.params,
        req.query,
        uri,
        reply
      );
    }
  }
});

我有以下内容:

{{1}}

有可能吗?

我正在使用hapi v6.9。

1 个答案:

答案 0 :(得分:0)

当你调用方法时,你必须传递一个回调方法。 https://github.com/hapijs/hapi/blob/83201d29bcc8c8edc46d9ecf4f1d51b7f3a3a4ff/lib/methods.js#L64

这是你的代码:

var hapi = require('hapi'),
    server = new hapi.Server('0.0.0.0', 5000);

server.method('getExport', function(reqParams, queryParams, hostUri, reply, next) {

  // ...
  reply.file('/tmp/test.js', {
    // Download as an attachment
    mode: 'attachment'
  });

  next();

});

server.route({
  method: 'GET',
  path: '/export',
  config: {
    handler: function(req, reply) {
      var uri = req.server.info.uri;

      server.methods.getExport(
        req.params,
        req.query,
        uri,
        reply,
        function () {
            console.log('ran');
        }
      );
    }
  }
});

server.start()

我强烈建议不要使用服务器方法,对于回复文件似乎有些过分。如果可能的话,我也强烈建议升级到最新版本的hapi。