1

reply.file( http://hapijs.com/api#replyfilepath-options ) をserver.method( http://hapijs.com/api#servermethodname-fn-options )に使用しようとしていますが、next関数がそうではないと表示されます持っているfile

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
      );
    }
  }
});

出来ますか?

私はhapi v6.9を使用しています。

4

1 に答える 1

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の最新バージョンにアップグレードすることを強くお勧めします。

于 2014-11-21T00:56:12.237 に答える