0

I have a very basic node.js server:

var http = require("http");

var server = http.createServer(function(req, res){
    console.log("Got request");

    res.writeHead(200, {"Content-Type":"text/plain"});
    res.end("Hi there!");
});

server.listen(3004);

I can access this via my browser and via sending a request from a Node.js client:

var http = require("http");

var options = {
    "hostname": "localhost",
    "port": 3004,
    "path": "/",
    "method": "GET"
};

var req = http.request(options, function(res){
    var data = "";

    res.on("data", function(chunk){
        data += chunk;
    });

    res.on("end", function(){
        console.log(data);
    });
});

req.on("error", function(e){
    console.log(e.stack);
});

req.end();

Now if I use the localtunnel package to "host" this server ( lt --port 3004 ), I now get my new hostname that I should be able to access it from on the internet. And indeed, I can easily access it from the browser.

However, if I try to send an https (not http because lt uses https) request from the Node.js client, with the new hostname, the request is refused and I get this error:

Error: connect ECONNREFUSED 138.197.63.247:3004 at TCPConnectWrap.afterConnect [as oncomplete]

So is it not possible to access a node.js web server when it is hosted with localtunnel? Or if there is (maybe using a 3rd part module) can someone please guide me on how that would be done as google is of absolutely no help. Thanks in advance.

EDIT1: I should note that on the server side, the error message does suggest "checking my firewall, but I have no idea what in my firewall could be blocking this, I'm not sure what to do. (Remember that this works when connecting from the browser??)

EDIT2: BTW, I tried to completely remove my firewall (got same result)

3 Answers3

1

Telebit is a similar service, albeit with slightly different goals, but I believe it will support your use case.

HTTPS (tls/ssl) is supported by way of Greenlock.js and Let's Encrypt, so you don't have to modify your node server or manually configure anything - the end-to-end encryption is handled automagically.

Install

curl https://get.telebit.io | bash

You can also install via npm... but that isn't the preferred install method at this time. There may be some caveats.

The random domain you get is attached to your account (hence the need for email).

Configure

./telebit http 4000

The general format is

./telebit <protocol> <port> [subdomain]

It's not just https, you can use it to tunnel anything over tls/ssl (plain tcp, ssh, openvpn, etc).

Custom domains are not yet a generally available feature, but they're on the horizon.

coolaj86
  • 74,004
  • 20
  • 105
  • 125
0

You need to use HTTPS module of Node.js in the server code to be able to access it via HTTPS protocol. Creating an HTTPS server requires a certificate and passphrase.

From the docs:

const https = require('https');
const fs = require('fs');

const options = {
  pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
  passphrase: 'sample'
};

https.createServer(options, (req, res) => {
  res.writeHead(200);
  res.end('hello world\n');
}).listen(8000);
tbking
  • 8,796
  • 2
  • 20
  • 33
  • I'm about to post a correct answer as I've just found out what I've been missing. Please either delete your post or edit it if you know a valid answer too –  Oct 21 '18 at 10:52
0

The issue is that you are not sending your request with the right port. When you use a tunneling service like localtunnel, it hosts your server as HTTPS. Now usually the port used for HTTPS is port 443, so the options object should look something like this:

var options = {
    "hostname": "whatever",
    "port": 443,  //HERE!
    "path": "/",
    "method": "GET"
};

The same goes for services like zeit or heroku that host your app from their cloud.

  • For those who are looking for an answer, this is the correct one. Change the port in the request options to 443. The below answer was previously upvoted as I thought it made sense, but when I came to try it, realised that this was actually the issue. In all cases, trying to tunnel an HTTPS server with localtunnel as suggested below simply won't work as localtunnel does not support SSL –  Oct 21 '18 at 10:59