我创建了一个简短的例子,我有一个dubt:
var request = require("request");
var url = "http://api.openweathermap.org/data/2.5/weather?q=turin&APPID=xxxxxxxxxxxxxxxxxxxxxx";
module.exports = function (callback) {
request(
{
url: url,
json: true
}, function (error, response, body) {
if (error) {
callback("Unable to fetch weather"); // callback function
} else {
callback("It is " + body.main.temp + " in " + body.name);
}
});
console.log("After request");
};从外部文件中,我需要这个模块:
var weather = require("./weather.js");
weather(function (currentWeather) {
console.log(currentWeather);
});在本例中,我调用一个weather模块,并得到一个callback函数(它是天气模块的参数),以便将都灵中的天气信息打印到命令行中。但工作怎么样?
https://stackoverflow.com/questions/40113342
复制相似问题