Javascript 如何发出NodeJS请求并将其导入另一个响应

Javascript 如何发出NodeJS请求并将其导入另一个响应,javascript,node.js,next.js,Javascript,Node.js,Next.js,所以,我正在做一个api,来查询某个需要api密钥的地图服务。我想将API密钥保持私有,因此在服务器上我自己的API中,我将调用一个http.request到映射服务,然后立即将其导入到对我自己的API用户的响应中 下面是示例代码来说明这一想法: 从“http”导入http; 导出默认函数处理程序(req、res){ http.request(`http://map.service.example.com/foo-bar`,(mapRes)=>mapRes.pipe(res)); } 但到目前

所以,我正在做一个api,来查询某个需要api密钥的地图服务。我想将API密钥保持私有,因此在服务器上我自己的API中,我将调用一个
http.request
到映射服务,然后立即将其导入到对我自己的API用户的响应中

下面是示例代码来说明这一想法:

从“http”导入http;
导出默认函数处理程序(req、res){
http.request(`http://map.service.example.com/foo-bar`,(mapRes)=>mapRes.pipe(res));
}
但到目前为止,上面的代码不起作用


欢迎使用任何其他可能的方法(可能使用fetch?)。

对于http.request,您必须调用其
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();
}
import http from "http";

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

您可以使用
get
方法:

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();
}
import http from "http";

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

它怎么不起作用?我想你必须提供更多的背景。包含一个如何发送HTTP请求的好例子。您需要一个
数据
侦听器和一个
结束
侦听器。我想你必须
结束
回复。