【问题标题】:Display response from HTTP request hapi.js 17.2.0显示来自 HTTP 请求 hapi.js 17.2.0 的响应
【发布时间】:2018-06-07 02:32:34
【问题描述】:

我正在尝试使用新的 HapiJS 17 显示页面,但它依赖于 http 请求。所以基本上我使用了一个休息 API,然后将其返回到新的响应工具包,即“h 工具包”,但它在控制台中给出了错误

错误:处理程序方法没有返回值、承诺或抛出错误

它还会出现浏览器错误

发生内部服务器错误

{"statusCode":500,"error":"Internal Server Error","message":"An internal server error occurred"}

下面是我的代码(为了简单起见,我尽量让它尽可能简单,不处理错误等等)

'use strict';

const Hapi = require('hapi');
const Request = require('request');

const server = Hapi.server({ 
  host: '127.0.0.1', 
  port: 8000,
  router: { stripTrailingSlash: true }
});

//Fix double slash issue
server.ext({
  type: 'onRequest',
  method: function (request, h) {
    var path = request.url.path.replace(/\/\//,'/');
    request.setUrl(path);
    return h.continue;
  }
});

server.route({
  method: 'GET',
  path:'/',
  handler: function (request, h) {
    return h.response('Welcome to HapiJS');
  }
});

server.route({
  method: 'GET',
  path:'/a',
  handler: function(req, h) {
    Request
      .get('https://httpbin.org/ip')
      .on('data', function(data) {
        return h.response('data:' + data);
      });
  }
});

server.start();

仅供参考,同样的事情在 Express 中也可以正常工作(我猜它也适用于 HapiJS 16

const Express = require('express');
const Request = require('request');
const app = Express();
const port = "8000";

app.get('/', function(req, res, next) {
  res.send('Hello world');
});

app.get('/a', function(req, res, next) {
  Request
    .get('https://httpbin.org/ip')
    .on('data', function(data) {
      res.send('data:' + data);
    });
});

app.listen(port, () => console.log(`Example app listening on port ${port}!`));

我在 HapiJS 16 中找到了这个示例

http://www.eloquentwebapp.com/comsume-restful-api/

【问题讨论】:

    标签: javascript node.js express promise hapijs


    【解决方案1】:

    错误提示您应该像这样返回 h.response 的承诺:

    server.route({
      method: 'GET',
      path:'/a',
      handler: (req, h) =>
        new Promise(
          (resolve,reject)=> 
            Request
            .get('https://httpbin.org/ip')
            .on('data', function(data) {
              resolve(h.response('data:' + data));
            })
        )
    });
    

    这仅适用于 Hapi.js 16

    但是;根据documentation,您无需返回任何内容,可以执行以下操作:

    server.route({
      method: 'GET',
      path:'/a',
      handler: (req, res) =>
        Request
        .get('https://httpbin.org/ip')
        .on('data', function(data) {
          res('data:' + data);
        })
    });
    

    【讨论】:

    • 两个答案都给出错误。第一个给出 Debug: internal, implementation, error ReferenceError: promise is not defined 。第二个给出 TypeError: res is not a function
    • @iEmad 将承诺更改为 Promise
    • 如何在我的函数中调整它?因为我想编写一个函数来从处理程序中调用它。重复使用
    【解决方案2】:

    因为,HapiJs v17.x 基于 async/await。 使用const Request = require('request-promise');

    请求-承诺:https://github.com/request/request-promise

    代码更改如下:

    server.route({
    method: 'GET',
    path:'/a',
    handler: async function(req, h) {
      let response = await Request.get('https://httpbin.org/ip');
      return response;
     }
    });
    

    【讨论】:

    • 它给出了一个错误:) SyntaxError: await is only valid in async function
    • 在函数中添加“异步”
    • 复制粘贴您的答案仍然会出错。 SyntaxError: Unexpected token )。当我尝试编辑代码以适应您所说的内容时,它给出了 错误:流必须有一个流 2 可读接口
    • 有问题
    • 使用 'request-promise' 而不是 'request' 链接:github.com/request/request-promise
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多