How to reply from outside of the hapi.js route handler

后端 未结 2 1871
甜味超标
甜味超标 2021-01-21 23:38

I have a hapi.js route where I want to defer the response. I\'ve tried storing the reply function and calling it later, or wrapping it in a promise, but hapi alway

相关标签:
2条回答
  • 2021-01-22 00:02

    If an error is thrown within your handler, hapi.js will immediately exit and give a 500 status code. Check if generateId() is a valid function.

    The rest of your code looks right for your third and fourth examples. reply().hold() is necessary to keep the connection open after handler returns.

    0 讨论(0)
  • 2021-01-22 00:09

    Since the version 8, Hapi supports Promised responses, so you can do :

    var respond = function(message) {
        return new Bluebird.Promise(function(resolve, reject) {
            setTimeout(() => {
                resolve(message);
            }, 2000);
        });
    };
    
    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, reply) {
            reply(respond("Hello buddy"));
        }
    });
    
    0 讨论(0)
提交回复
热议问题