i create an short example and i have a 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");
};
And from external file, i required this module:
var weather = require("./weather.js");
weather(function (currentWeather) {
console.log(currentWeather);
});
In this case, i call a weather
module and i get a callback
function ( it is an argument of weather module ) for print into command line the weather in Turin. But how it's work?