最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTTP request callback on Node JS - Stack Overflow

programmeradmin4浏览0评论

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 of callback 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
Add a ment  | 

1 Answer 1

Reset to default 4

In 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();

}
发布评论

评论列表(0)

  1. 暂无评论