Node: http2: code examples does not work on Windows with port 80

Created on 16 Jul 2017  路  6Comments  路  Source: nodejs/node

  • Version: master with #14239
  • Platform: Windows 7 x 64
  • Subsystem: http2

Refs: https://github.com/nodejs/node/pull/14239 (http2.md)

The first examples from "Core API" chapter do not work properly on Windows with port 80 but work with other ports.

I've added some output and repeated request for easier debugging. Here are the code and output with port 81:

Server:

const http2 = require('http2');

// Create a plain-text HTTP/2 server
const server = http2.createServer();

server.on('stream', (stream, headers) => {
  console.log('headers: ', headers);
  stream.respond({
    'content-type': 'text/html',
    ':status': 200
  });
  stream.end('<h1>Hello World</h1>');
});

server.listen(81);

Client:

const http2 = require('http2');

function test() {
  const client = http2.connect('http://localhost:81');

  const req = client.request({ ':path': '/' });

  req.on('response', (headers) => {
    console.log(headers[':status']);
    console.log(headers['date']);
  });

  let data = '';
  req.setEncoding('utf8');
  req.on('data', (d) => data += d);
  req.on('end', () => { client.destroy(); console.log('data: ', data); });
  req.end();
}

setInterval(test, 2000);

Server output:

(node:7960) ExperimentalWarning: The http2 module is an experimental API.
headers:  { ':scheme': 'http',
  ':authority': 'localhost:81',
  ':method': 'GET',
  ':path': '/' }
headers:  { ':scheme': 'http',
  ':authority': 'localhost:81',
  ':method': 'GET',
  ':path': '/' }
headers:  { ':scheme': 'http',
  ':authority': 'localhost:81',
  ':method': 'GET',
  ':path': '/' }

Client output:

(node:6732) ExperimentalWarning: The http2 module is an experimental API.
200
Sun, 16 Jul 2017 13:40:19 GMT
data:  <h1>Hello World</h1>
200
Sun, 16 Jul 2017 13:40:21 GMT
data:  <h1>Hello World</h1>
200
Sun, 16 Jul 2017 13:40:23 GMT
data:  <h1>Hello World</h1>

When I change the port in both examples into 80, I get:
Server output:

(node:4792) ExperimentalWarning: The http2 module is an experimental API.

Client output:

(node:1784) ExperimentalWarning: The http2 module is an experimental API.
data:
data:
data:

FWIW, I've tried to see what was going on with TCPView:
http2
The first two is the Server, the last two are Client requests and they are trying to connect the 443 port.

doc http2

All 6 comments

Will look into it! Thank you! :-)

ping @vsemozhetbyt 鈥斅爄s this still the case for you?

@apapirovski Yes, the behavior from the first post remains with Node.js 8.7.0 or the last nightly build.

@vsemozhetbyt Could you try the same with http or net when you have a moment?

@apapirovski Let me know if I use wrong test examples.
Server:

'use strict';

const http = require('http');

const hostname = 'localhost';
const port = 81;

const server = http.createServer((req, res) => {
  console.log(req.headers);

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Client:

'use strict';

const http = require('http');

http.get({
  hostname: 'localhost',
  port: 81,
  path: '/',
}, (res) => {
  console.log(res.headers);
});

Port 81, Server:

Server running at http://localhost:81/
{ host: 'localhost:81', connection: 'close' }

Port 81, Client:

{ 'content-type': 'text/plain',
  date: 'Thu, 19 Oct 2017 12:30:28 GMT',
  connection: 'close',
  'content-length': '12' }

Port 80, Server:

Server running at http://localhost:80/
{ host: 'localhost', connection: 'close' }

Port 80, Client:

{ 'content-type': 'text/plain',
  date: 'Thu, 19 Oct 2017 12:31:35 GMT',
  connection: 'close',
  'content-length': '12' }

Thanks so much for testing http too! I think I have a rough lead on what's going on. Will update later tonight when I have a moment to confirm whether I'm right or not.

Was this page helpful?
0 / 5 - 0 ratings