leonormes
leonormes

Reputation: 1029

Why are there 2 requests from my browser?

I have a simple node server. All it does is log the req.headers and res (I am learning!).

let http = require('http');

function handleIncomingRequest(req, res) {
    console.log('---------------------------------------------------');
    console.log(req.headers);
    console.log('---------------------------------------------------');
    console.log();
    console.log('---------------------------------------------------');
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify( {error: null}) + '\n');
}


let s = http.createServer(handleIncomingRequest);
s.listen(8080);

When I use curl to test the server it sends 1 request. When I use chrome it sends 2 different requests.

  { host: 'localhost:8080',
  connection: 'keep-alive',
  'cache-control': 'max-age=0',
  'upgrade-insecure-requests': '1',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  'accept-encoding': 'gzip, deflate, sdch, br',
  'accept-language': 'en-GB,en;q=0.8' }

and

{ host: 'localhost:8080',
  connection: 'keep-alive',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  accept: 'image/webp,image/*,*/*;q=0.8',
  referer: 'http://localhost:8080/',
  'accept-encoding': 'gzip, deflate, sdch, br',
  'accept-language': 'en-GB,en;q=0.8' }

This is in incognito mode as in normal mode there are 3 requests! What is the browser doing and why?

Upvotes: 0

Views: 1504

Answers (1)

Danny_ds
Danny_ds

Reputation: 11406

Hard to tell without seeing the full transaction data (for example, what was the request, i.e. what came after GET or POST - and what were the answers from the server).

But it could be caused by the 'upgrade-insecure-requests': '1' header:

When a server encounters this preference in an HTTP request’s headers, it SHOULD redirect the user to a potentially secure representation of the resource being requested.

See this.


accept: 'image/webp,image/*,*/*;q=0.8'

On the other hand, the second request is probably for an image only, most likely the favicon.ico or a (bigger) icon for iPad/iPhone maybe (that could explain the 3 requests). You should check out the full request data to be sure.

You can use F12 en select network in the browser to see what's really happening.

Upvotes: 1

Related Questions