首页 > nodejs express 发起get请求 回调处理

nodejs express 发起get请求 回调处理

var express = require('express');
var app = express();
var os = require('os');
var http = require('http');


app.get('/', function(req, res){  
  var ip = req.headers["X-Forwarded-For"] || req.connection.remoteAddress;  
  var uIP = ip.match(/\d+\.\d+\.\d+\.\d+/); 
   httpQQMap(uIP,function(info){
         res.send(info);
    })
  
});


function httpQQMap(ip,call){
   http.get("http://apis.map.qq.com/ws/location/v1/ip?ip="+ ip +"&key="+ qqqkey +"", function(ress) {
    call(ress.text);
  }).on('error', function(e) {
    call(e.message);   
  });
}

页面输出空白,发起get请求 回调处理 如何将回调的数据返回到页面呢

控制台 network

ress.text 打印出的 是 undefined

ress 输出


 headers:
  { server: 'HTTP Load Balancer/1.0',
    date: 'Wed, 16 Dec 2015 04:55:40 GMT',
    'content-type': 'application/json; charset=utf-8',
    'content-length': '64',
    connection: 'close',
    'set-cookie': [ 'mpuv=CuGrLlZw7sy3OTLYDb0WAg==; expires=Thu, 15-Dec-16 04:5
:40 GMT; domain=map.qq.com; path=/' ] },
 rawHeaders:
  [ 'Server',
    'HTTP Load Balancer/1.0',
    'Date',
    'Wed, 16 Dec 2015 04:55:40 GMT',
    'Content-Type',
    'application/json; charset=utf-8',
    'Content-Length',
    '64',
    'Connection',
    'close',
    'Set-Cookie',
    'mpuv=CuGrLlZw7sy3OTLYDb0WAg==; expires=Thu, 15-Dec-16 04:55:40 GMT; domain
map.qq.com; path=/' ],
 trailers: {},
 rawTrailers: [],
 upgrade: false,
 url: '',
 method: null,
 statusCode: 200,
 statusMessage: 'OK',
 client:
  Socket {
    _connecting: false,
    _hadError: false,
    _handle:
     TCP {
       _externalStream: {},
       fd: -1,
       reading: true,
       owner: [Circular],
       onread: [Function: onread],
       onconnection: null,
       writeQueueSize: 0 },
    _parent: null,
    _host: 'apis.map.qq.com',
    _readableState:
     ReadableState {
       objectMode: false,
       highWaterMark: 16384,
       buffer: [],
       length: 0,
       pipes: null,
       pipesCount: 0,
       flowing: true,
       ended: false,
       endEmitted: false,
       reading: false,
       sync: false,
       needReadable: true,
       emittedReadable: false,
       readableListening: false,
       defaultEncoding: 'utf8',
       ranOut: false,
       awaitDrain: 0,
       readingMore: false,
       decoder: null,
       encoding: null,
       resumeScheduled: false },
    readable: true,
    domain: null,
    _events:
     { end: [Object],
       finish: [Function: onSocketFinish],
       _socketEnd: [Function: onSocketEnd],
       free: [Function: onFree],
       close: [Object],
       agentRemove: [Function: onRemove],
       drain: [Function: ondrain],
       error: [Function: socketErrorListener],
       data: [Function: socketOnData] },
    _eventsCount: 9,
    _maxListeners: undefined,
    _writableState:
     WritableState {
       objectMode: false,
       highWaterMark: 16384,
       needDrain: false,
       ending: false,
       ended: false,
       finished: false,
       decodeStrings: false,
       defaultEncoding: 'utf8',
       length: 0,
       writing: false,
       corked: 0,
       sync: false,
       bufferProcessing: false,
       onwrite: [Function],
       writecb: null,
       writelen: 0,
       bufferedRequest: null,
       lastBufferedRequest: null,
       pendingcb: 0,
       prefinished: false,
       errorEmitted: false },
    writable: true,
    allowHalfOpen: false,
    destroyed: false,
    bytesRead: 345,
    _bytesDispatched: 134,
    _sockname: null,
    _pendingData: null,
    _pendingEncoding: '',
    parser:
     HTTPParser {
       '0': [Function: parserOnHeaders],
       '1': [Function: parserOnHeadersComplete],
       '2': [Function: parserOnBody],
       '3': [Function: parserOnMessageComplete],
       _headers: [],
       _url: '',
       socket: [Circular],
       incoming: [Circular],
       maxHeaderPairs: 2000,
       onIncoming: [Function: parserOnIncomingClient] },
    _httpMessage:
     ClientRequest {
       domain: null,
       _events: [Object],
       _eventsCount: 1,
       _maxListeners: undefined,
       output: [],
       outputEncodings: [],
       outputCallbacks: [],
       outputSize: 134,
       writable: true,
       _last: true,
       chunkedEncoding: false,
       shouldKeepAlive: false,
       useChunkedEncodingByDefault: false,
       sendDate: false,
       _removedHeader: {},
       _contentLength: 0,
       _hasBody: true,
       _trailer: '',
       finished: true,
       _headerSent: true,
       socket: [Circular],
       connection: [Circular],
       _header: 'GET /ws/location/v1/ip?ip=27.115.98.238&key=OB4BZ-D4W3U-B7VVO-
PJWW-6TKDJ-WPB77 HTTP/1.1\r\nHost: apis.map.qq.com\r\nConnection: close\r\n\r\n
,
       _headers: [Object],
       _headerNames: [Object],
       _onPendingData: null,
       agent: [Object],
       socketPath: undefined,
       method: 'GET',
       path: '/ws/location/v1/ip?ip=27.115.98.238&key=OB4BZ-D4W3U-B7VVO-4PJWW-6
KDJ-WPB77',
       parser: [Object],
       res: [Circular] },
    read: [Function],
    _consuming: true },
 _consuming: false,
 _dumped: false,
 req:
  ClientRequest {
    domain: null,
    _events: { error: [Function] },
    _eventsCount: 1,
    _maxListeners: undefined,
    output: [],
    outputEncodings: [],
    outputCallbacks: [],
    outputSize: 134,
    writable: true,
    _last: true,
    chunkedEncoding: false,
    shouldKeepAlive: false,
    useChunkedEncodingByDefault: false,
    sendDate: false,
    _removedHeader: {},
    _contentLength: 0,
    _hasBody: true,
    _trailer: '',
    finished: true,
    _headerSent: true,
    socket:
     Socket {
       _connecting: false,
       _hadError: false,
       _handle: [Object],
       _parent: null,
       _host: 'apis.map.qq.com',
       _readableState: [Object],
       readable: true,
       domain: null,
       _events: [Object],
       _eventsCount: 9,
       _maxListeners: undefined,
       _writableState: [Object],
       writable: true,
       allowHalfOpen: false,
       destroyed: false,
       bytesRead: 345,
       _bytesDispatched: 134,
       _sockname: null,
       _pendingData: null,
       _pendingEncoding: '',
       parser: [Object],
       _httpMessage: [Circular],
       read: [Function],
       _consuming: true },
    connection:
     Socket {
       _connecting: false,
       _hadError: false,
       _handle: [Object],
       _parent: null,
       _host: 'apis.map.qq.com',
       _readableState: [Object],
       readable: true,
       domain: null,
       _events: [Object],
       _eventsCount: 9,
       _maxListeners: undefined,
       _writableState: [Object],
       writable: true,
       allowHalfOpen: false,
       destroyed: false,
       bytesRead: 345,
       _bytesDispatched: 134,
       _sockname: null,
       _pendingData: null,
       _pendingEncoding: '',
       parser: [Object],
       _httpMessage: [Circular],
       read: [Function],
       _consuming: true },
    _header: 'GET /ws/location/v1/ip?ip=27.115.98.238&key=OB4BZ-D4W3U-B7VVO-4PJ
W-6TKDJ-WPB77 HTTP/1.1\r\nHost: apis.map.qq.com\r\nConnection: close\r\n\r\n',
    _headers: { host: 'apis.map.qq.com' },
    _headerNames: { host: 'Host' },
    _onPendingData: null,
    agent:
     Agent {
       domain: null,
       _events: [Object],
       _eventsCount: 1,
       _maxListeners: undefined,
       defaultPort: 80,
       protocol: 'http:',
       options: [Object],
       requests: {},
       sockets: [Object],
       freeSockets: {},
       keepAliveMsecs: 1000,
       keepAlive: false,
       maxSockets: Infinity,
       maxFreeSockets: 256 },
    socketPath: undefined,
    method: 'GET',
    path: '/ws/location/v1/ip?ip=27.115.98.238&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKD
-WPB77',
    parser:
     HTTPParser {
       '0': [Function: parserOnHeaders],
       '1': [Function: parserOnHeadersComplete],
       '2': [Function: parserOnBody],
       '3': [Function: parserOnMessageComplete],
       _headers: [],
       _url: '',
       socket: [Object],
       incoming: [Circular],
       maxHeaderPairs: 2000,
       onIncoming: [Function: parserOnIncomingClient] },
    res: [Circular] } }
:\zan\www\node\koa\demo\node_modules\express\lib\response.js:242
 var body = JSON.stringify(val, replacer, spaces);
                 ^

ypeError: Converting circular structure to JSON
   at Object.stringify (native)
   at ServerResponse.json (E:\zan\www\node\koa\demo\node_modules\express\lib\re
ponse.js:242:19)
   at ServerResponse.send (E:\zan\www\node\koa\demo\node_modules\express\lib\re
ponse.js:151:21)
   at ClientRequest.<anonymous> (E:\zan\www\node\koa\demo\index.js:36:9)
   at ClientRequest.g (events.js:260:16)
   at emitOne (events.js:77:13)
   at ClientRequest.emit (events.js:169:7)
   at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:415:21

   at HTTPParser.parserOnHeadersComplete (_http_common.js:88:23)
   at Socket.socketOnData (_http_client.js:305:20)



把控制台的network贴出来?打印ress看看?

【热门文章】
【热门文章】