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

孤街醉人 提交于 2019-12-02 02:26:45

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.

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"));
    }
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!