Hi I am trying to get a response via a http using the callback method. But I get an error saying callback is not a function.
module.exports.ipLookup = function (res, callback) {
var http = require('http');
var str = '';
var options = {
host: 'ip-api',
port: 80,
path: '/json/',
method: 'POST'
};
var str= "";
var req = http.request(options, function (res) {
res.on('data', function (body) {
str += body;
});
res.on('end', function () {
callback(str);
});
});
req.end();
return str;
}
What is should to id return the json api response via ip-api. If anyone can help me on this it would be greatly appreciated.
Hi I am trying to get a response via a http using the callback method. But I get an error saying callback is not a function.
module.exports.ipLookup = function (res, callback) {
var http = require('http');
var str = '';
var options = {
host: 'ip-api.',
port: 80,
path: '/json/',
method: 'POST'
};
var str= "";
var req = http.request(options, function (res) {
res.on('data', function (body) {
str += body;
});
res.on('end', function () {
callback(str);
});
});
req.end();
return str;
}
What is should to id return the json api response via ip-api.. If anyone can help me on this it would be greatly appreciated.
Share Improve this question asked Nov 22, 2015 at 5:29 matttsmattts 3522 gold badges8 silver badges21 bronze badges 2-
1
How are you using/invoking
ipLookup()
? The value ofcallback
will be determined there. – Jonathan Lonowski Commented Nov 22, 2015 at 5:55 - @JonathanLonowski, thanks for the reply. I am requiring the js file onto another file. e.g. var lookup = require('ip'); using lookup.ip(); – mattts Commented Nov 22, 2015 at 8:35
1 Answer
Reset to default 4In the other file you are loading the function from you need to ensure the callback parameter is used. See snippet below
var Lookup = require('./ip');
Lookup.ipLookup(function (response) {
console.log(response) // check if response is valid
})
You need to change the exported function to only accept one parameter as the first parameter is not used inside function body like so.
module.exports.ipLookup = function (callback) {
var http = require('http');
var str = '';
var options = {
host: 'ip-api.',
port: 80,
path: '/json/',
method: 'POST'
};
var req = http.request(options, function (res) {
res.on('data', function (body) {
str += body;
});
res.on('end', function () {
return callback(str);
});
});
req.end();
}