2

I am try to make a simple GET request in Node.js and the request takes 3-5 seconds to resolve whereas the same request in a browser or REST client takes ~400ms. The server to which I am making the request is controlled by our server team, but before I bother them with request/resource monitoring, I was going to ping the community to see if there were any "hey, check this setting first" kind of tips you guys could offer.

The code essentially forwards incoming requests to our server:

http.createServer(function (req, res) { http.request({ host: "our.private.host", port: 8080, path: req.url, headers: req.headers }, function () { res.end("DONE: " + Date.now()); }).end(); }).listen(8001); 

I open my browser and type in the following URL:

http://localhost:8001/path/to/some/resource

... which gets forwarded on to the final destination:

http://our.private.host:8080/path/to/some/resource

Everything is working fine and I am getting the response I want, but it takes 3-5 seconds to resolve. If I paste the final destination URL directly in the browser or a REST client, it resolves quickly. I don't know much about our server, unfortunately - but I am looking more for node tips at this point. Note, the request pool isn't maxed out as I am only making 1 request at a time from my local machine.

4
  • 1
    I'd suggest using some sort of network trace program to see the timing of the network activity on your node server. Is it taking a long time to do a DNS lookup, to connect, to get the response, to process the response, etc... Then, you may know where to look next. Commented Apr 21, 2015 at 2:37
  • 1
    Maybe browser is using some kind of caching and your request is not. Commented Apr 21, 2015 at 3:37
  • @jfriend00 - thanks for the info. If you could wrap your suggestion into the answer box, maybe suggest some network tracing tools and elaborate a little on the process, I'll accept it. That's really what I was looking for. Commented Apr 21, 2015 at 17:30
  • I know this is closed, but using JSFiddle I confirmed that the latency is happening on our server - 4.5 seconds between receiving the request and starting the response. I also discovered today that using cURL via command line has the same delay. Any thoughts as to why before I bug the server team? Commented Apr 24, 2015 at 4:25

1 Answer 1

1

The first step is gather some info on where the request is taking its time by looking at the exact timing of the network activity on your node server. You can do that by getting a tool that watches all network activity. I personally use Fiddler, but I know that WireShark is popular too.

Once that tools is installed and active, you can then see how long all these various steps in the process of your request are taking:

  1. DNS request to resolve target IP address
  2. Time to connect to the target server
  3. Time to send the http request
  4. Time to receive the http request
  5. Time to send response back to original request

Understanding which of these operations is much longer than expected will give you an idea where to look further for the problem.

FYI, there are pre-built tools such as nginx that can do this type of proxying by just setting some values in a configuration file without any custom coding.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the info. Saved me a lot of research time and really set me down the right path to finding Charles Proxy, which supports PC, Mac, and Linux.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.