【问题标题】:stopping postman after request is done请求完成后停止邮递员
【发布时间】:2021-05-12 16:34:30
【问题描述】:

我正在尝试使用 node.js 创建天气 API。在我的控制器文件中,我有为 /check 路由运行的代码。

controller.js:

//Check Weather
exports.check = (req, res) => {
    UserModel.check(req.body.city)
};

model.js:

//Check Weather
function getData(city) { 

url = "something";
request(url, function (err, response, body) {
if(err){
    console.log('error:', error);
} else {
    console.log('body:', body);
}
});
} 

exports.check = (city) => {

city = city.toLowerCase();
let values = getData(city);
console.log(city);

return(values);
};

路线:

app.post('/check', [
    UsersController.check
]);

当我运行它时,它会正常运行,并且正确的内容会记录在控制台中。但是,当我在 Postman 中发送请求并显示 console.log 后,Postman 似乎被挂断了,如图 pic 所示。有没有办法让 Postman 在返回或 console.log 后停止发送请求?

【问题讨论】:

    标签: node.js rest routes postman


    【解决方案1】:

    邮递员正在等待服务器的响应。您的代码当前未发送任何响应,因此邮递员在等待时似乎“挂断”了。尝试将UserModel.check(req.body.city) 的行更改为res.send(UserModel.check(req.body.city)),以便它将从您的UserModel.check 函数返回的数据作为响应发送回来。或者,如果您不想发回返回值,您可以在函数调用后添加res.send(PutWhateverYouWantSentHere)

    【讨论】:

      猜你喜欢
      • 2022-06-16
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2020-05-22
      • 2015-10-26
      • 2015-05-06
      相关资源
      最近更新 更多