hoxy icon indicating copy to clipboard operation
hoxy copied to clipboard

What if the server response timeOut or 404

Open aemonge opened this issue 9 years ago • 12 comments

How to control the server timeOut or statusCode === 404 in the 'response' phase ??

aemonge avatar Nov 03 '15 23:11 aemonge

You can check response.statusCode during the response phase.

greim avatar Nov 04 '15 16:11 greim

I'm not getting any statusCode..... is their a timeOut setting to make it shorter. Or why am I not getting the statusCode ?

  function responseInterceptor(request, response) {
    console.log('+++++++++++++++++++ Response ++++++++++++++++++++');
    console.log(arguments, response.statusCode); // < === Not getting 404 nor timeOut
    console.log('+++++++++++++++++++++++++++++++++++++++++++++++++');
  }

  function requestInterceptor(request, response) {
    // .........
  }

  function attachInterceptors() {
    proxy.intercept({
      phase: 'request',
      as: 'json'
    }, requestInterceptor);

    proxy.intercept({
      phase: 'response',
      as: 'json'
    }, responseInterceptor);
  }

  proxy = hoxy.createServer({
    reverse: 'https://example.com/'
    // tls: {
      // key: fs.readFileSync('./my-server.key.pem'),
      // cert: fs.readFileSync('./my-server.crt.pem')
    // }
  }).listen(5000);

  attachInterceptors();

aemonge avatar Nov 04 '15 17:11 aemonge

Is it logging undefined?

greim avatar Nov 04 '15 17:11 greim

  '1':
   Response {
     domain: null,
     _events: { log: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     _data: { statusCode: 200, headers: {}, slow: {} },
     phase: 'request' },
  '2':
   Cycle {
     domain: null,
     _events: { log: [Function] },
     _eventsCount: 1,
     _maxListeners: undefined,
     _proxy:
      Proxy {
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        _reverse: 'https://non-existing-example.com/',
        _tls: undefined,
        _intercepts: [Object],
        _server: [Object] },
     _request:
      Request {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _data: [Object],
        _populated: true,
        phase: 'request' },
     _response:
      Response {
        domain: null,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _data: [Object],
        phase: 'request' },
     _phase: 'request' } }

aemonge avatar Nov 04 '15 18:11 aemonge

That looks incomplete. What does it log if you just do console.log(response.statusCode)?

greim avatar Nov 04 '15 20:11 greim

Nothing is been logged. I've managed to resolve this issues by login in the 'response-sent' phase. Yeah, I've trim the request part of the argument cause I didn't tough it was usefull.

So if you intend to support the response.statusCode on response phase, then this issue isn't fixed, and I would gladdly provide you an example of it

But if you intend no to support it, and instead encorage user to use the response-sent phase, then you should make a note on documentation ;)

I would be more than happy to help you either ways, I think hoxy is amazing ;) !!! And you're very kind ^^

aemonge avatar Nov 04 '15 20:11 aemonge

But specifically, when you say nothing was logged, do you mean it logged undefined, or was the console.log() statement never reached, or something else?

greim avatar Nov 05 '15 16:11 greim

console.log() never reached, as if the timeOut is way to big, or something like that

aemonge avatar Nov 05 '15 20:11 aemonge

The output pasted above looks like a fragment of arguments is being printed from console.log(arguments, response.statusCode). The status code should appear immediately after it. However it looks like you didn't paste the entire output, which is why I was asking.

greim avatar Nov 06 '15 15:11 greim

So no logging the server's timeOut (waited my whole lunch, so about an hour or so )

This is my code

#!/usr/bin/env node
/*eslint no-unused-vars: 0*/
'use strict';

(function () {
  var hoxy = require('hoxy'), proxy;

  function responseSentInterceptor(request, response) {
    console.log('====================== RESPONSE SENT INTERCEPTOR');
    console.log(request, response);
  }

  function responseInterceptor(request, response) {
    console.log('====================== RESPONSE INTERCEPTOR');
    console.log(request, response);
  }

  function requestInterceptor(request, response) {
    console.log('======================= REQUEST INTERCEPTOR');
    console.log(request, response);
  }

  function attachInterceptors() {
    proxy.intercept({
      phase: 'request',
      as: 'json'
    }, requestInterceptor);

    proxy.intercept({
      phase: 'response',
      as: 'json'
    }, responseInterceptor);

    proxy.intercept({
      phase: 'response-sent'
    }, responseSentInterceptor);
  }

  proxy = hoxy.createServer({
    reverse: 'http://thisIsNot.a.real.server'
  }).listen(5000);

  attachInterceptors();
}());

Output

======================= REQUEST INTERCEPTOR
Request {
  domain: null,
  _events: { log: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _data: 
   { slow: {},
     httpVersion: '1.1',
     headers: 
      { 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0',
        accept: 'application/json, text/javascript',
        'accept-language': 'en-US,es;q=0.8,en;q=0.5,es-ES;q=0.3',
        'accept-encoding': 'gzip, deflate',
        dnt: '1',
        channel: '49',
        version: '2',
        'x-requested-with': 'XMLHttpRequest',
        'content-type': 'application/json; charset=UTF-8',
        referer: 'http://localhost:3000/',
        'content-length': '268',
        cookie: 'JSESSIONID=0000RWu-GDpXC4SsvFPvl5PFTkq:191dudcmv',
        connection: 'keep-alive',
        pragma: 'no-cache',
        'cache-control': 'no-cache',
        host: 'thisisnot.a.real.server' },
     protocol: 'http:',
     hostname: 'thisisnot.a.real.server',
     method: 'POST',
     url: '/auth/validate',
     source: 
      JsonReader {
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: {},
        _eventsCount: 0,
        _maxListeners: undefined,
        _obj: [Object] } },
  _populated: true,
  phase: 'request' } Response {
  domain: null,
  _events: { log: [Function] },
  _eventsCount: 1,
  _maxListeners: undefined,
  _data: { statusCode: 200, headers: {}, slow: {} },
  phase: 'request' }

aemonge avatar Nov 06 '15 19:11 aemonge

So I hope this clarify my issue =^.^=

And thanks again !! (:

aemonge avatar Nov 06 '15 19:11 aemonge

Can you add

proxy.log('error warn debug', process.stderr);
proxy.log('info', process.stdout);

and let me know if it logs any errors or anything?

greim avatar Nov 06 '15 20:11 greim