[英]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.