控制台日志重复

时间:2017-01-26 16:09:17

标签: node.js console.log

我不确定为什么这行代码(在底部)

console.log(user_location);//this prints out 2 console responses

在此代码中

    var net = require('net');
var express = require('express');
var app = express();
var port = process.env.PORT || 8000;

// require request-ip and register it as middleware
var requestIp = require('request-ip');

// to convert the ip into geolocation coords
var geoip2 = require('geoip2');

// you can override which attirbute the ip will be set on by
// passing in an options object with an attributeName
app.use(requestIp.mw({ attributeName : 'myCustomAttributeName' }));

// respond to all requests
app.use(function(req, res) {

    // use our custom attributeName that we registered in the middleware
    //var ip = req.myCustomAttributeName;
    var ip = '207.97.227.239';
    //console.log(ip);
    //console.log('requestip ip is:  ' + ip);

    // https://nodejs.org/api/net.html#net_net_isip_input
    var ipType = net.isIP(ip); // returns 0 for invalid, 4 for IPv4, and 6 for IPv6
    res.end('Hello, your ip address is ' + ip + ' and is of type IPv' + ipType + '\n');

    //geoip2
    geoip2.init();// init the db
    //console.log(ip);
        geoip2.lookupSimple(ip, function(error, result) {//67.183.57.64, 207.97.227.239
            var user_location = result.city;
          if (error) {
            console.log("Error: %s", error);
          }
          else if (user_location) {//replaced result with user_location
            console.log(user_location);//this prints out 2 console responses
          }
        });
});

我不确定为什么它在控制台中打印user_location两次......我对nodejs有些新意见......所以我不确定为什么会发生这种情况......有人可以更多关于nodejs的知识可以指出我做错了什么?

0 个答案:

没有答案
相关问题