我是Web开发的新手。我正在尝试使app.js中的请求响应在全局范围内可用,以及与app.js位于同一目录级别的其他文件可用。
我尝试搜索并遇到了诺言,但是我不确定如何在这种情况下实现它。
var respbody = ""
app.get('/',(req, res) => {
let url = "www.xyz.com";
request.post(url, function (error, response, body) {
respbody = JSON.parse(body);
console.log(respbody); //This is fine because it's still in the scope of the request.
}
});
console.log(respbody) //This gives empty string, I believe because its executed before the request. How can I make it return the actual response?
此外,我希望能够使用module.exports在另一个文件中导出此响应正文的属性。有可能吗?
我希望输出是json主体有效负载,但是它给了我空字符串。