Sunday, 23 April 2017

Why Can't I Connect To This NodeJS HTTPS Hosted On Google Cloud?

I'm having trouble connecting to an https NodeJS server hosted on linux. I first created the keyfile (jims-key.pem) and certificate (jims-csr.pem) using the instructions here to make example.key and example.crt.

Then I create the javascript file for the https server:

const https = require('https');
const fs = require('fs');
const options = {
  key: fs.readFileSync('ssl_cert/example.key'),
  cert: fs.readFileSync('ssl_cert/example.cert')
};
https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(443);
console.log('listening...');

I then run this file with:

node server.js

and it prints out "listening...".

If I disown this process and open a new shell window in my server linux instance, I can connect to the https server it like this:

curl -k https://localhost:443

and I can see the response, "hello world".

It also show the response when I use the instance's public static ip address instead of localhost:

curl -k https://104.xxx.xx.xx:443

However, when I try to connect from my local terminal or a web browser I am unable to connect, and I get a "connection refused" error:

curl: (7) Failed to connect to 10.142.0.2 port 443: Connection refused

How can I set up the HTTPS server do that my clients can connect to it?

The server is a google cloud compute engine instance running this:

Debian GNU/Linux 8.7 (jessie) node v6.10.1 npm v3.10.10



via Jim