K
K
kr_ilya2019-04-27 07:55:54
Node.js
kr_ilya, 2019-04-27 07:55:54

Why is node.js missing a function?

This code returns the necessary information to the browser, but it takes a long time - 2-4 seconds.

var rp = require('request-promise');
var i = '91.105.236.7';

app.get('/', function(req, res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
var options = {
    uri: 'http://api.ipapi.com/' + i,
    qs: {
        access_key: '98eeeb1c1233456743840f9195d' // -> uri + '?access_token=xxxxx%20xxxxx'
    }
};
 
rp(options)
    .then(function (repos) {
    	console.log(repos);
    	res.send(repos);
    })
    .catch(function (err) {
        // API call failed...
    });
});

And such code returns emptiness to the browser, but after a while the desired answer is displayed in the console. The browser responds quickly, usually in less than a second.
var rp = require('request-promise');
var i = '91.105.236.7';
var geo = geoIP(i);

app.get('/', function(req, res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

  res.send(geo);
});
function geoIP(i){
var options = {
    uri: 'http://api.ipapi.com/' + i,
    qs: {
        access_key: '98eeeb1c73191023211233121233840f9195d' // -> uri + '?access_token=xxxxx%20xxxxx'
    }
};
 
rp(options)
    .then(function (repos) {
    	console.log(repos);
        return repos
    })
    .catch(function (err) {
        // API call failed...
    });
}

The code with this function call also does not work correctly:
app.get('/', function(req, res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
var geo = geoIP(i);
  res.send(geo);
});

I suspect that the function simply does not have time to be processed, because there is a call to the api of a third-party site. Is this related to how node.js works? (new to node.js)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-04-27
@kr_ilya

so your geoIP function does not return anything, and the output to the console does not depend on the request at all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question