What is the work of callback argument in NodeJs "module.exports"?
-
I’m learning nodeJS. I don’t know what is the purpose of
callback
argument in
module.exports
. It pretty much look like
return
and
console.log
here is my code
var request = require('request');
module.exports = function(location,callback) {
var encodedUrl = encodeURIComponent(location); //set url var url = "http://api.openweathermap.org/data/2.5/weather?q="+encodedUrl+"&APPID=575991c6b55a139d32139652d78bc8b8&units=metric";
//location check
if(!location) {
callback("No location provided");
} else {
callback('location is '+location);
}//request
request({
url: url,
json: true
}, function (error,response, body) {
if(error) {
callback('Unable To Fetch Weather');
} else {
//console.log(JSON.stringify(body, null, 4));
callback("Current Temperature in "+body.name+" "+body.main.temp + '\xB0!!!');
}
});
}; -
I’m learning nodeJS. I don’t know what is the purpose of
callback
argument in
module.exports
. It pretty much look like
return
and
console.log
here is my code
var request = require('request');
module.exports = function(location,callback) {
var encodedUrl = encodeURIComponent(location); //set url var url = "http://api.openweathermap.org/data/2.5/weather?q="+encodedUrl+"&APPID=575991c6b55a139d32139652d78bc8b8&units=metric";
//location check
if(!location) {
callback("No location provided");
} else {
callback('location is '+location);
}//request
request({
url: url,
json: true
}, function (error,response, body) {
if(error) {
callback('Unable To Fetch Weather');
} else {
//console.log(JSON.stringify(body, null, 4));
callback("Current Temperature in "+body.name+" "+body.main.temp + '\xB0!!!');
}
});
};