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)

4

3 回答 3

2

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.

于 2018-10-25T05:15:58.890 回答
1

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);
于 2018-10-13T22:18:34.640 回答
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.

于 2018-10-21T10:56:16.587 回答