【发布时间】:2023-03-09 13:25:01
【问题描述】:
我在编写正确的回调时遇到了困难。 当用户向“/city”服务器发出请求时,必须向第三方服务请求查询数据。为此,我执行 http.request() 来请求和接收数据(没有问题)。然后我需要将数据传输到 getCity() 函数之外的 res.send() 中。
我不知道。请解释一下解决方案和工作原理。谢谢!
var getCity = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
//what to do?
});
}
app.get('/city', function (req, res) {
var request = http.request(options, getCity);
request.end();
res.send("ok"); //need sending str from getCity instead of "ok";
});
【问题讨论】:
标签: javascript node.js http express