TypingPanda TypingPanda - 2 months ago 15
Node.js Question

expressjs server http request returning empty response

I am scratching my head off on this.

I am new to expressjs and right now I am developing a website that acting as a proxy to fetch data from other parcel website.

Here is what I get from the postman.

enter image description here

Heigh lights about this screenshot:


  1. the request method is get with two url params

  2. the request headers are displayed (6 in total)

  3. the response is shown as an html page in the response field with status code 200



To mimick above requesting behaviour I created following expresssjs code:

'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();
}


and following is the debugging out put:

-------------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] }


As you can see, I have used exact same request method, host,path, headers, but end up with a 200 status where response body is empty.

What am I missing? What should I do?

Thanks!!!

Answer

So what ever you are doing is fine, its just you are missing a "/"

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

this is the code you have : 'chs': { host: 'www.zhonghuan.com.au', path: clientController.do?getlogisticsDetail&parcelNo=${pGuid}, }

Comments