简体   繁体   English

如何将 Node.js http.IncomingMessage 转换为获取请求 object?

[英]How can I turn a Node.js http.IncomingMessage into a Fetch Request object?

I'm trying to work with the eBay APIs.我正在尝试使用 eBay API。 It's a small personal project that just needs to run locally, and although I know C#, I'm much more comfortable with Javascript so I'm looking for ways to get this done in JS.这是一个只需要在本地运行的小型个人项目,虽然我知道 C#,但我对 Javascript 更满意,所以我正在寻找在 JS 中完成这项工作的方法。

I found this promising looking eBay Node API with browser support .我发现这个具有浏览器支持的看起来很有前途的 eBay 节点 API Browser support is something I'm looking for, but it also says that浏览器支持是我正在寻找的东西,但它也说

A Proxy server is required to use the API in the Browser.在浏览器中使用 API 需要代理服务器。

They give an example file for a proxy server that is a Cloudflare worker.他们为作为 Cloudflare 工作者 的代理服务器提供了一个示例文件

I'm trying to translate that into something I can run in Node locally using the basic Node HTTP server.我正在尝试使用基本的 Node HTTP 服务器将其转换为可以在 Node 本地运行的内容。 I've been following through it and am doing OK so far, figured out the different ways to access the headers and check them, etc., but now I'm at the point where the proxy server is making the proxy request to the eBay APIs.我一直在关注它并且到目前为止做得很好,想出了访问标题和检查它们的不同方法等,但现在我正处于代理服务器向 eBay 发出代理请求的地步蜜蜂。 The way the example file is set up, it seems as though the Cloudflare worker intercepts the HTTP request and by default treats it as a Fetch Request .示例文件的设置方式似乎 Cloudflare 工作人员拦截了 HTTP 请求,并默认将其视为 Fetch Request So then when it goes to pass on the request, it just kind of clones it (but is replacing the headers with "cleaned" headers):因此,当它传递请求时,它只是克隆它(但是用“清理”的标头替换标头):

// "recHeaders" is an object that is _most_ of the original
// request headers, with a few cleaned out, and "fetchUrl"
// is the true intended URL to query at eBay

const newReq = new Request(event.request, {
    "headers": recHeaders
});

const response = await fetch(encodeURI(fetchUrl), newReq);

The problem is that I don't have a Fetch Request to clone - since I'm running a Node HTTP server, what is event.request in the example code is for me a http.IncomingMessage .问题是我没有要克隆的获取Request - 因为我正在运行节点 HTTP 服务器,所以示例代码中的event.request对我来说是http.IncomingMessage

So how can I turn that into a Fetch Request ?那么我怎样才能把它变成一个 Fetch Request呢? I'm guessing at the very least there's stuff in the message body that needs to get passed along, if not other properties I'm not even aware of...我猜至少消息正文中有一些东西需要传递,如果不是我什至不知道的其他属性...

I don't mind doing the work, ie reading the stream to pull out the body, and then putting that into the Request object somehow (or do I even need to do that? Can I just pipe the stream from the IncomingMessage directly into a Request somehow?), but what else besides the body do I need to make sure I get from the IncomingMessage to put into the Request ? I don't mind doing the work, ie reading the stream to pull out the body, and then putting that into the Request object somehow (or do I even need to do that? Can I just pipe the stream from the IncomingMessage directly into a以某种方式Request ?),但是除了正文之外,我还需要确保从IncomingMessage中获取到Request吗?

How do I turn a Node http.IncomingMessage into a Fetch Request and be sure to include all relevant parts?如何将 Node http.IncomingMessage转换为 Fetch Request并确保包含所有相关部分?

I've made a simple function to convert.我制作了一个简单的 function 进行转换。

const convertIncomingMessageToRequest = (req: ExpressRequest): Request => {
  var headers = new Headers();
  for (var key in req.headers) {
    if (req.headers[key]) headers.append(key, req.headers[key] as string);
  }
  let request = new Request(req.url, {
    method: req.method,
    body: req.method === 'POST' ? req.body : null,
    headers,
  })
  return request
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否会在node.js http.request中的“ http.IncomingMessage”上发出“错误”事件? - Will the 'error' event ever be emitted on 'http.IncomingMessage' in a node.js http.request? 如何手动创建 http.IncomingMessage stream? - How can I manually create http.IncomingMessage stream? 节点请求模块Http.IncomingMessage不发出事件 - Node request module Http.IncomingMessage not emitting events NodeJS请求模块 - http.IncomingMessage上的正文? - NodeJS request module - body on http.IncomingMessage? 如何为express.static模拟http.ServerResponse和http.IncomingMessage - How to mock http.ServerResponse and http.IncomingMessage for express.static 我如何使用 node.js http 开玩笑地模拟传入的 http 请求? - How I can mock an incomming http request in jest using node.js http? 如何向我的 Node.js HTTP 请求添加自定义值以供 Express 使用 - How can I add a custom value to my Node.js HTTP request for Express to consume 如何使用 Node.js 中的请求库获取原始 HTTP 消息正文? - How can I get the raw HTTP message body using the request library in Node.js? 如何通过Node.JS Server响应非标准(非HTTP)请求? - How can I respond back to a non standard(non HTTP) request over Node.JS Server? 如何修复Node.js http发布请求或处理错误以更有效地进行调试? - How can I fix a Node.js http post request or handle errors to more efficiently debug?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM