node.js - hapi 错误 : handler method did not return a value, 一个promise,或者抛出一个错误

标签 node.js hapi.js

我有一个使用版本 17 的 Hapi 的 NodeJS 应用程序,它使用返回 map 图像的 Web 服务,但是,当运行下面的代码时,我收到以下错误:

Debug: internal, implementation, error
    Error: handler method did not return a value, a promise, or throw an error
    at module.exports.internals.Manager.execute (C:\map\node_modules\hapi\lib\toolkit.js:52:29)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

var Hapi = require('hapi'),
server = new Hapi.Server({
    host: '0.0.0.0',
    port: 8080,
    routes: {
        cors: true
    },
    SphericalMercator = require('sphericalmercator'),
    sm = new SphericalMercator({ size: 256 }),
    prequest = require('request').defaults({ encoding = null });

var wmsUrl = "http://localhost:8088/service/wms?SERVICE=WMS&REQUEST=GetMap&VERSION=1.1.1&STYLES=&FORMAT=image%2Fpng&HEIGHT=383&WIDTH=768&SRS=EPSG%3A3857";

server.route({
    method: 'GET',
    path: '/{layers}/{z}/{x}/{y}.png',
    handler: async (request, h) => {
        var bbox = sm.bbox(request.params., request.params.y, request.params.z, false, '00000');
        var theUrl = `${wmsUrl}&BBOX=${bbox.join(',')}&LAYERS=${decodeURIComponent(request.params.layers)}`;
        prequest.get(theUrl, function (err, res, body) {
            h.response(body).header('Cache-Control'), 'public, max-age=2629000').header('Content-Type', 'image/png');
        });
    }
});

server.start();

我做错了什么?

我正在用手机写这篇文章,因为我现在正在使用 PC,无法访问互联网,如果我因为自动更正器而遗漏了什么或拼错了什么,请随时指向它,我会进行编辑进行更正。

最佳答案

如果您查看 hapi docs对于生命周期方法,它指出:

Each lifecycle method must return a value or a promise that resolves into a value.

因此,只需在您的处理程序中返回一些内容:

 handler: async (request, h) => {
        var bbox = sm.bbox(request.params., request.params.y, request.params.z, false, '00000');
        var theUrl = `${wmsUrl}&BBOX=${bbox.join(',')}&LAYERS=${decodeURIComponent(request.params.layers)}`;
        prequest.get(theUrl, function (err, res, body) {
            h.response(body).header('Cache-Control'), 'public, max-age=2629000').header('Content-Type', 'image/png');
        });

        return null; // or a Plain Value: string, number, boolean. Could be a Promise etc, more on the link above.
    }

如果你不返回任何它不喜欢的未定义的东西。

编辑:

如果你想返回prequest的body结果,你可以将它包装在一个Promise中并返回:

handler: async (request, h) => {
    ...

    const promise = new Promise((resolve, reject) => {
        prequest.get(theUrl, function (err, res, body) {
            if (err) {
                reject(err);
            } else {
                const response = h.response(body)
                    .header('Cache-Control', 'public, max-age=2629000')
                    .header('Content-Type', 'image/png');

                resolve(response);
            }

        });
    });

    return promise;
}

关于node.js - hapi 错误 : handler method did not return a value, 一个promise,或者抛出一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48953596/

相关文章:

javascript - 如何在 Joi 中捕获验证失败的回调

node.js - 为什么此查询可以在 mongo shell 中运行,但不能在 Node mongo 驱动程序中运行?

javascript - 确保 MongoDB 中的点赞索引

javascript - 为什么 hapi.js 很特别?

javascript - 如何在 hapi 0.8.4 中添加路由

node.js - 如何在 hapi js 中以流形式回复文件?

node.js - 使用 Express 和 Mongoose 更新嵌套数组

javascript - DELETE api 请求响应 404 错误

node.js - Visual Studio代码: "' tslib' cannot be found"error in Angular project

node.js - 哈皮斯 : Performance tuning for lots of concurrent requests