繁体   English   中英

如何使 NodeJS 请求和 pipe 成为另一个响应

[英]How to make a NodeJS request and pipe it into another response

所以,我正在做一个 api 来查询某个需要 API 密钥的 map 服务。 I want to keep the API key private so in my own api on the server, I will call a http.request to the map service, then immediately pipe it into the response to my own api user.

这是说明这个想法的示例代码:

import http from "http";

export default function handler(req, res) {
    http.request(`http://map.service.example.com/foo-bar`, (mapRes) => mapRes.pipe(res));
}

但到目前为止,上面的代码不起作用。

欢迎任何其他可能的方式(可能有 fetch ?)。

对于http.request到 go,您必须调用其end方法:

import http from "http";

export default function handler(req, res) {
    const mapReq = http.request(`http://map.service.example.com/foo-bar`, (mapRes) => mapRes.pipe(res));
    mapReq.end();
}

或者

您可以使用get方法:

import http from "http";

export default function handler(req, res) {
    http.get(`http://map.service.example.com/foo-bar`, (mapRes) => mapRes.pipe(res));
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM