expressjs服务器http请求返回空响应

时间:2016-09-05 14:54:07

标签: node.js http express request

我对此感到头疼。

我是expressjs的新手,现在我正在开发一个网站,作为从其他包裹网站获取数据的代理。

这是我从邮递员那里得到的。

enter image description here

关于此屏幕截图的亮灯:

  1. 请求方法是使用两个url params
  2. 显示请求标题(总共6个)
  3. 响应显示为响应字段中的html页面,状态代码为200
  4. 为了模仿上面的请求行为,我创建了以下expresssjs代码:

        'use strict';
    var http = require("http");
    //var https = require("https");
    
    
    
    exports.fetchPackageFromWeb = function (req, res, next) {
        //res.send('hello world');
        //fetch package info
        console.log('-------------1');
        let pGuid = req.query.pGuid;
        console.log('pGuid::' + pGuid);
    
        var optDic = {
            'auexpress': {
                host: 'www.auexpress.com.au',
                path: `/TOrderQuery_Service.aspx?OrderId=${pGuid}`,
            },
            'chs': {
                host: 'www.zhonghuan.com.au',
                path: `clientController.do?getlogisticsDetail&parcelNo=${pGuid}`,
            }
        }
    
    
        console.log('-------------2');
        var options = {
            host: optDic[req.query.company].host,
            path: optDic[req.query.company].path,
            method: 'GET',
            headers: {
                'Upgrade-Insecure-Requests' : '1'
                ,'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
                ,'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
                , 'Accept-Encoding': 'gzip, deflate, sdch'          
                , 'Accept-Language': 'en-AU,en;q=0.8,en-US;q=0.6'
                ,'Cookie' : 'JSESSIONID=5D11F26C0B41F60481C2426623C197B5'  
    
            }
        };
        console.log(options);
    
        var port =  http;
    
        var InnerReq = port.request(options, function (innerRes) {
            var output = '';
            console.log('-------------3');
            //console.log(options.host + ':' + innerRes.statusCode);
            //innerRes.setEncoding('utf8');
            console.log('--------------------------start res');
            //console.log(innerRes);
    
            innerRes.on('data', function (chunk) {
                output += chunk;
            });
    
            innerRes.on('end', function () {
                console.log('-------------4');
                console.log('output::' + output);
                console.log('innerRes::')
                console.log(innerRes);
                res.send(output)
            });
        });
    
        InnerReq.on('error', function (err) {
            //res.send('error: ' + err.message);
            console.log(err);
        });
    
        InnerReq.end();
    }
    

    以下是调试输出:

    -------------1
    pGuid::900016077939
    -------------2 (Request Option)
    { host: 'www.zhonghuan.com.au',
      path: 'clientController.do?getlogisticsDetail&parcelNo=900016077939',
      method: 'GET',
      headers: 
       { 'Upgrade-Insecure-Requests': '1',
         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
         Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
         'Accept-Encoding': 'gzip, deflate, sdch',
         'Accept-Language': 'en-AU,en;q=0.8,en-US;q=0.6',
         Cookie: 'JSESSIONID=5D11F26C0B41F60481C2426623C197B5' } }
      express:router dispatching GET /static/images/cmp_logo_chs.png +58ms
      express:router query  : /static/images/cmp_logo_chs.png +0ms
      express:router expressInit  : /static/images/cmp_logo_chs.png +0ms
      express:router jsonParser  : /static/images/cmp_logo_chs.png +0ms
      express:router serveStatic  : /static/images/cmp_logo_chs.png +0ms
      express:router dispatching GET /fonts/glyphicons-halflings-regular.woff2 +1ms
      express:router query  : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router expressInit  : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router jsonParser  : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router serveStatic  : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router dispatching GET /favicon.ico +0ms
      express:router query  : /favicon.ico +0ms
      express:router expressInit  : /favicon.ico +6ms
      express:router jsonParser  : /favicon.ico +0ms
      express:router serveStatic  : /favicon.ico +0ms
      express:router <anonymous>  : /static/images/cmp_logo_chs.png +0ms
      express:router serveStatic  : /static/images/cmp_logo_chs.png +0ms
      express:router <anonymous>  : /fonts/glyphicons-halflings-regular.woff2 +3ms
      express:router serveStatic  : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router <anonymous>  : /favicon.ico +7ms
      express:router serveStatic  : /favicon.ico +0ms
      express:router trim prefix (/static) from url /static/images/cmp_logo_chs.png +0ms
      express:router serveStatic /static : /static/images/cmp_logo_chs.png +0ms
      express:router trim prefix (/fonts) from url /fonts/glyphicons-halflings-regular.woff2 +1ms
      express:router serveStatic /fonts : /fonts/glyphicons-halflings-regular.woff2 +0ms
      express:router trim prefix (/favicon.ico) from url /favicon.ico +1ms
      express:router <anonymous> /favicon.ico : /favicon.ico +0ms
    -------------3
    --------------------------start res
    -------------4
    output::
    innerRes::
    IncomingMessage {
      _readableState: 
       ReadableState {
         objectMode: false,
         highWaterMark: 16384,
         buffer: [],
         length: 0,
         pipes: null,
         pipesCount: 0,
         flowing: true,
         ended: true,
         endEmitted: true,
         reading: false,
         sync: true,
         needReadable: false,
         emittedReadable: false,
         readableListening: false,
         defaultEncoding: 'utf8',
         ranOut: false,
         awaitDrain: 0,
         readingMore: false,
         decoder: null,
         encoding: null,
         resumeScheduled: false },
      readable: false,
      domain: null,
      _events: 
       { end: [ [Function: responseOnEnd], [Function] ],
         data: [Function] },
      _eventsCount: 2,
      _maxListeners: undefined,
      socket: 
       Socket {
         _connecting: false,
         _hadError: false,
         _handle: null,
         _parent: null,
         _host: 'www.zhonghuan.com.au',
         _readableState: 
          ReadableState {
            objectMode: false,
            highWaterMark: 16384,
            buffer: [],
            length: 0,
            pipes: null,
            pipesCount: 0,
            flowing: true,
            ended: false,
            endEmitted: false,
            reading: true,
            sync: false,
            needReadable: true,
            emittedReadable: false,
            readableListening: false,
            defaultEncoding: 'utf8',
            ranOut: false,
            awaitDrain: 0,
            readingMore: false,
            decoder: null,
            encoding: null,
            resumeScheduled: false },
         readable: false,
         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] },
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: 
          WritableState {
            objectMode: false,
            highWaterMark: 16384,
            needDrain: false,
            ending: true,
            ended: true,
            finished: true,
            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: true,
            errorEmitted: false },
         writable: false,
         allowHalfOpen: false,
         destroyed: true,
         bytesRead: 130,
         _bytesDispatched: 509,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         parser: null,
         _httpMessage: 
          ClientRequest {
            domain: null,
            _events: [Object],
            _eventsCount: 1,
            _maxListeners: undefined,
            output: [],
            outputEncodings: [],
            outputCallbacks: [],
            outputSize: 0,
            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 clientController.do?getlogisticsDetail&parcelNo=900016077939 HTTP/1.1\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-AU,en;q=0.8,en-US;q=0.6\r\nCookie: JSESSIONID=5D11F26C0B41F60481C2426623C197B5\r\nHost: www.zhonghuan.com.au\r\nConnection: close\r\n\r\n',
            _headers: [Object],
            _headerNames: [Object],
            _onPendingData: null,
            agent: [Object],
            socketPath: undefined,
            method: 'GET',
            path: 'clientController.do?getlogisticsDetail&parcelNo=900016077939',
            parser: null,
            res: [Circular] },
         read: [Function],
         _consuming: true,
         _idleNext: null,
         _idlePrev: null,
         _idleTimeout: -1 },
      connection: 
       Socket {
         _connecting: false,
         _hadError: false,
         _handle: null,
         _parent: null,
         _host: 'www.zhonghuan.com.au',
         _readableState: 
          ReadableState {
            objectMode: false,
            highWaterMark: 16384,
            buffer: [],
            length: 0,
            pipes: null,
            pipesCount: 0,
            flowing: true,
            ended: false,
            endEmitted: false,
            reading: true,
            sync: false,
            needReadable: true,
            emittedReadable: false,
            readableListening: false,
            defaultEncoding: 'utf8',
            ranOut: false,
            awaitDrain: 0,
            readingMore: false,
            decoder: null,
            encoding: null,
            resumeScheduled: false },
         readable: false,
         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] },
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: 
          WritableState {
            objectMode: false,
            highWaterMark: 16384,
            needDrain: false,
            ending: true,
            ended: true,
            finished: true,
            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: true,
            errorEmitted: false },
         writable: false,
         allowHalfOpen: false,
         destroyed: true,
         bytesRead: 130,
         _bytesDispatched: 509,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         parser: null,
         _httpMessage: 
          ClientRequest {
            domain: null,
            _events: [Object],
            _eventsCount: 1,
            _maxListeners: undefined,
            output: [],
            outputEncodings: [],
            outputCallbacks: [],
            outputSize: 0,
            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 clientController.do?getlogisticsDetail&parcelNo=900016077939 HTTP/1.1\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-AU,en;q=0.8,en-US;q=0.6\r\nCookie: JSESSIONID=5D11F26C0B41F60481C2426623C197B5\r\nHost: www.zhonghuan.com.au\r\nConnection: close\r\n\r\n',
            _headers: [Object],
            _headerNames: [Object],
            _onPendingData: null,
            agent: [Object],
            socketPath: undefined,
            method: 'GET',
            path: 'clientController.do?getlogisticsDetail&parcelNo=900016077939',
            parser: null,
            res: [Circular] },
         read: [Function],
         _consuming: true,
         _idleNext: null,
         _idlePrev: null,
         _idleTimeout: -1 },
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: 
       { server: 'Apache-Coyote/1.1',
         'content-length': '0',
         date: 'Mon, 05 Sep 2016 14:34:27 GMT',
         connection: 'close' },
      rawHeaders: 
       [ 'Server',
         'Apache-Coyote/1.1',
         'Content-Length',
         '0',
         'Date',
         'Mon, 05 Sep 2016 14:34:27 GMT',
         'Connection',
         'close' ],
      trailers: {},
      rawTrailers: [],
      upgrade: false,
      url: '',
      method: null,
      statusCode: 400,
      statusMessage: 'Bad Request',
      client: 
       Socket {
         _connecting: false,
         _hadError: false,
         _handle: null,
         _parent: null,
         _host: 'www.zhonghuan.com.au',
         _readableState: 
          ReadableState {
            objectMode: false,
            highWaterMark: 16384,
            buffer: [],
            length: 0,
            pipes: null,
            pipesCount: 0,
            flowing: true,
            ended: false,
            endEmitted: false,
            reading: true,
            sync: false,
            needReadable: true,
            emittedReadable: false,
            readableListening: false,
            defaultEncoding: 'utf8',
            ranOut: false,
            awaitDrain: 0,
            readingMore: false,
            decoder: null,
            encoding: null,
            resumeScheduled: false },
         readable: false,
         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] },
         _eventsCount: 8,
         _maxListeners: undefined,
         _writableState: 
          WritableState {
            objectMode: false,
            highWaterMark: 16384,
            needDrain: false,
            ending: true,
            ended: true,
            finished: true,
            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: true,
            errorEmitted: false },
         writable: false,
         allowHalfOpen: false,
         destroyed: true,
         bytesRead: 130,
         _bytesDispatched: 509,
         _sockname: null,
         _pendingData: null,
         _pendingEncoding: '',
         parser: null,
         _httpMessage: 
          ClientRequest {
            domain: null,
            _events: [Object],
            _eventsCount: 1,
            _maxListeners: undefined,
            output: [],
            outputEncodings: [],
            outputCallbacks: [],
            outputSize: 0,
            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 clientController.do?getlogisticsDetail&parcelNo=900016077939 HTTP/1.1\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-AU,en;q=0.8,en-US;q=0.6\r\nCookie: JSESSIONID=5D11F26C0B41F60481C2426623C197B5\r\nHost: www.zhonghuan.com.au\r\nConnection: close\r\n\r\n',
            _headers: [Object],
            _headerNames: [Object],
            _onPendingData: null,
            agent: [Object],
            socketPath: undefined,
            method: 'GET',
            path: 'clientController.do?getlogisticsDetail&parcelNo=900016077939',
            parser: null,
            res: [Circular] },
         read: [Function],
         _consuming: true,
         _idleNext: null,
         _idlePrev: null,
         _idleTimeout: -1 },
      _consuming: true,
      _dumped: false,
      req: 
       ClientRequest {
         domain: null,
         _events: { error: [Function] },
         _eventsCount: 1,
         _maxListeners: undefined,
         output: [],
         outputEncodings: [],
         outputCallbacks: [],
         outputSize: 0,
         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: null,
            _parent: null,
            _host: 'www.zhonghuan.com.au',
            _readableState: [Object],
            readable: false,
            domain: null,
            _events: [Object],
            _eventsCount: 8,
            _maxListeners: undefined,
            _writableState: [Object],
            writable: false,
            allowHalfOpen: false,
            destroyed: true,
            bytesRead: 130,
            _bytesDispatched: 509,
            _sockname: null,
            _pendingData: null,
            _pendingEncoding: '',
            parser: null,
            _httpMessage: [Circular],
            read: [Function],
            _consuming: true,
            _idleNext: null,
            _idlePrev: null,
            _idleTimeout: -1 },
         connection: 
          Socket {
            _connecting: false,
            _hadError: false,
            _handle: null,
            _parent: null,
            _host: 'www.zhonghuan.com.au',
            _readableState: [Object],
            readable: false,
            domain: null,
            _events: [Object],
            _eventsCount: 8,
            _maxListeners: undefined,
            _writableState: [Object],
            writable: false,
            allowHalfOpen: false,
            destroyed: true,
            bytesRead: 130,
            _bytesDispatched: 509,
            _sockname: null,
            _pendingData: null,
            _pendingEncoding: '',
            parser: null,
            _httpMessage: [Circular],
            read: [Function],
            _consuming: true,
            _idleNext: null,
            _idlePrev: null,
            _idleTimeout: -1 },
         _header: 'GET clientController.do?getlogisticsDetail&parcelNo=900016077939 HTTP/1.1\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\nAccept-Encoding: gzip, deflate, sdch\r\nAccept-Language: en-AU,en;q=0.8,en-US;q=0.6\r\nCookie: JSESSIONID=5D11F26C0B41F60481C2426623C197B5\r\nHost: www.zhonghuan.com.au\r\nConnection: close\r\n\r\n',
         _headers: 
          { 'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
            accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'accept-encoding': 'gzip, deflate, sdch',
            'accept-language': 'en-AU,en;q=0.8,en-US;q=0.6',
            cookie: 'JSESSIONID=5D11F26C0B41F60481C2426623C197B5',
            host: 'www.zhonghuan.com.au' },
         _headerNames: 
          { 'upgrade-insecure-requests': 'Upgrade-Insecure-Requests',
            'user-agent': 'User-Agent',
            accept: 'Accept',
            'accept-encoding': 'Accept-Encoding',
            'accept-language': 'Accept-Language',
            cookie: 'Cookie',
            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: 'clientController.do?getlogisticsDetail&parcelNo=900016077939',
         parser: null,
         res: [Circular] },
      read: [Function] }
    

    正如您所看到的,我使用了完全相同的请求方法,主机,路径,标题,但最终得到了200状态,其中响应主体为空。

    我错过了什么?我该怎么办?

    感谢!!!

1 个答案:

答案 0 :(得分:0)

所以你所做的一切都很好,只是你错过了一个“/”

  • 路径:/clientController.do?getlogisticsDetail&parcelNo=${pGuid}

这是您拥有的代码: 'chs':{             主持人:'www.zhonghuan.com.au',             路径:clientController.do?getlogisticsDetail&parcelNo=${pGuid},         }