Hapi.js代理-在回复之前更改响应 [英] Hapi.js Proxy - change the response before reply

查看:75
本文介绍了Hapi.js代理-在回复之前更改响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我正在研究的新项目测试Hapi.js 我的目标是在我的Hapi服务器上触发一个请求,该请求将使用Hapi代理在远程服务器上触发另一个请求,并且响应将只是远程响应中的特定属性

I'm trying to test Hapi.js for a new project I'm working on My goal is to trigger a request on my Hapi server and this request will trigger another request on a remote server using Hapi proxy, and the response will be only specific properties from the remote response

要对此进行测试,必须向/api/type3/name发出请求,该请求将调用对GitHub API的API调用以获取用户对象

To test this a request to /api/type3/name must be made which will invoke an API call to GitHub's API to get the user object

代码:

server.route({
    method: 'GET',
    path: '/api/type3/{name}',
    handler: {
      proxy: {
        mapUri: function(request,callback){
            callback(null,'https://api.github.com/users/'+request.params.name);
        },
        onResponse: function(err, res, request, reply, settings, ttl){
           console.log(res); 
           reply(res);
        },
        passThrough: true,
        xforward: true
      }
    }
});

以上代码的响应是GitHub的响应对象 现在,我需要保存此响应,以便可以对其进行操作并仅返回我需要的

The response from the above code is the response object from GitHub Now I need to save this response so I can manipulate it and return only what I need

但是当我调试响应对象时,它包含很多数据,而且我无法在其中找到响应有效载荷

But when I debug the response object it contains a lot of data and I can't find the response payload inside of it

那么如何从响应对象中仅提取来自GitHub的响应?

So how to extract only the response from GitHub from the response object ?

非常感谢

推荐答案

在上述残骸中,您可以

payload = payload.toString();

要将缓冲区转换为实际数据.

To convert the buffer to actual data.

这篇关于Hapi.js代理-在回复之前更改响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆