Hapi错误:处理程序方法没有返回值,承诺或抛出错误
我有一个使用Hapi的版本17的NodeJS应用程序,它使用一个返回地图图像的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的生命周期方法,它说:
每个生命周期方法都必须返回一个值或一个解析为值的promise。
所以,只需在处理程序中返回一些内容:
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;
}
Hapi错误:处理程序方法没有返回值,承诺或抛出错误
我有一个使用Hapi的版本17的NodeJS应用程序,它使用一个返回地图图像的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的生命周期方法,它说:
每个生命周期方法都必须返回一个值或一个解析为值的promise。
所以,只需在处理程序中返回一些内容:
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;
}