NodeJS http2/https


#1

Hello!

I cannot get a simple NodeJS/http2 example to work;

const fs    = require('fs')
const http2 = require('http2')

const options = {
    cert: fs.readFileSync('./server.crt'),
    key:  fs.readFileSync('./server.key')
}

const server = http2.createSecureServer(options)

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

server.listen(8080)

node --expose-http2 index.js

When navigating to page, I get the error ECONNRESET: Request could not be proxied!

When calling the server locally, using curl http://localhost:8080 it answers with curl: (52) Empty reply from server so atleast the server answers.

My guess is that it’s probably because there’s either an error in my code (but it has been verified to work locally) or that http2/https is incompatable with the cloud9 proxy.

Thanks in advance for any information / help on this.


#2

You are right in that the Cloud9 proxy does not work with HTTPS. There is, however, a workaround without having to change your code. Create a new file with any name in /etc/nginx/sites-available/. Put the following in it:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_ssl_verify off;
    proxy_pass https://localhost:8000;
}

The Cloud9 proxy uses HTTPS to secure the connection to the proxy, but it is then downgraded to HTTP. What the above config does is upgrades the request to HTTPS and forwards it to your server. It also turns off certificate validation to your server, because the certificate you’re using can’t be used with localhost. Make sure to run ln -s /etc/nginx/sites-available/<your-file-here> /etc/nginx/sites-enabled/<whatever-filename> to enable the configuration, then run sudo service nginx restart to have nginx pick up the new config. When you visit your URL in the browser, you should be able to use HTTPS, but the certificate will be the one used by the Cloud9 proxy.

As for why cURL gave you an empty reply, I can’t be sure, but it might have to do with the fact that you requested the page using localhost over http.

As a side note, I also recommend using a framework like koa or express to simplify security, routing, and building responses, along with the middleware available on npm


#3

Thanks @dannytech!

The Cloud9 proxy uses HTTPS to secure the connection to the proxy, but it is then downgraded to HTTP. What the above config does is upgrades the request to HTTPS and forwards it to your server.

I never thought of using my own nginx proxy, very clever!

As for why cURL gave you an empty reply, I can’t be sure, but it might have to do with the fact that you requested the page using localhost over http.

probably!

As a side note, I also recommend using a framework like koa or express to simplify security, routing, and building responses, along with the middleware available on npm

Yes, definitely! This is just a miniamlistic example to get it working.

Thanks again for your generous answer!