Make Your Raspberry Pi Web Server Available on the Internet with Node.js

web server image

Note: Please see my updated article entitled How to Host a Raspberry Pi Web Server on the Internet with ngrok.  The current article you are reading describes a Node module (localtunnel) which no longer works reliably.

In Create a Web Server in Node without any Code, we used the http-server npm module to create a web server and serve up files in record time.  Today I will share a way you can take the web server you built and make it available beyond your local network and on the public Internet.  This is pretty cool trick, but I recommend that you use this with caution as this will open up a hole in your router/firewall when you make your web server available to the world.

Once again, we are using Node.js and so this will work on a variety of platforms.  I am focusing here on making this work in the context of Raspberry Pi which is an amazing little machine for IoT applications and everything else under the sun. Ok, let’s get rolling.

Log into your RasPi from a Windows system by launching a Windows Remote Desktop Connection.  (Hit the Windows key on your keyboard and type Remote Desktop Connection to get started.)  This method of remote connection works since we installed xrdp on the RasPi as part of our Beginners Guide to Installing Node.js on a Raspberry Pi 2. Of course, you can connect the RasPi directly to a monitor/keyboard/mouse or use other remote options such as an X Server and come in through Linux, Mac OS X, etc. too.

After you are in the RasPi, launch a terminal session and type the following:

$ sudo npm install -g localtunnel

This will install the npm package called localtunnel globally so it is available as a command anywhere on the RasPi rather than just in the current directory.  The localtunnel package npm page summarizes the mission of this package well:

localtunnel exposes your localhost to the world for easy testing and sharing! No need to mess with DNS or deploy just to have others test out your changes.

Now that we have localtunnel installed, let’s see what kind of parameters it accepts.  You will notice that we invoke localtunnel with “lt” which is the command that is created as part of the global installation.

$ lt --help
Usage: lt --port [num] <options>

Options:
  -h, --host        Upstream server providing forwarding  [default: "http://localtunnel.me"]
  -s, --subdomain   Request this subdomain
  -l, --local-host  Tunnel traffic to this host instead of localhost, override Host header to this host
  -o, --open        opens url in your browser
  -p, --port        Internal http server port  [required]
  --help            Show this help and exit  [boolean]
  --version         Show version number  [boolean]

We see a very nice, simple help page.  I’m going to use the single-character options (e.g. -p) instead of the long option parameter names (e.g. –port).  Please note that you must use two dashes (–port) when using long option names rather than one dash in order for localtunnel to understand and honor the long option name parameters you are passing.

We first need to get our Web server back up and running so that we can make it available to the world.  Launch a new terminal session (leave the other first terminal session open still) and enter the following commands:

$ cd Public
$ http-server

This will make the index.html file and any other files you have residing in your /home/pi/Public directory available to be served up by the web server.

Next, jump back over to the original terminal session you launched so we can get localtunnel working.  Let’s tunnel through to our web server running on port 8080 and launch the local web browser to view our web server from the outside world.  (You can follow along by reviewing the options that are listed in the help info above.)

$ lt -p 8080 -o

Fantastic!  Upon launching the above command, we are assigned a random subdomain such as http://jxloprztp.localhost.me and we now have our web server available on the real Internet.  Go to another machine besides your RasPi such as a laptop/desktop or cell phone so that you can see for yourself that your website has hit the big time and is available everywhere! 🙂

You can now hit Ctrl-C in the terminal window running localtunnel to break the tunnel connection. Pretty amazing technology, eh?

Local tunnel has some other options. For example, you can choose your own subdomain rather than being assigned a random subdomain.  Let’s try that now:

$lt -p 8080 -s mypizzaweb -o

Assuming nobody is actively using the mypizzaweb subdomain, you can now navigate to it by typing “http://mypizzaweb.localtunnel.me” in your Web browser address bar.

Once again, be sure to hit Ctrl-C in the terminal window running localtunnel to break the tunnel connection.  It’s the wild west out there.

That’s it until next time!

Follow Dave Johnson on Twitter to stay up to date on the latest tutorials and tech articles.

Share

13 thoughts on “Make Your Raspberry Pi Web Server Available on the Internet with Node.js

    1. Great point – this is certainly another option, especially with the auto-reconnect functionality. Thanks for pointing this out for the benefit of the my readers.

  1. Hi,

    I’m having a web application with 6 pages, some javascript on each page running from nodejs. If i open it on local network I receive everything, the css and the scripts, but if i try to open it in browser(from the raspberry pi) with the url assigned by localtunnel i only get the html. If i try the url in my pc machine browser i get this: “no active client for ‘alexwirelessrobot'”. Any thoughts?

    1. Hi Alex, I think localtunnel is down at the moment since I am not able to run the “lt” command. You could try ngrok instead to achieve your goal. Here are the steps:

      1. $ npm install -g ngrok
      2. Start your Node web application. I’m assuming it is running on port 3000 for the next steps.
      3. Confirm your web server works by launching http://localhost:3000/
      4. $ ngrok http 3000
      5. Copy and paste the ngrok URL printed to the console in the previous step.
      6. Launch the ngrok URL and everything should work on your local system, and on other computers, mobile devices. etc. outside your local area network.

      Hopefully this works for you!

      1. Hi Dave,

        Appreciate the reply.
        I’m facing one more problem at the moment. I’m trying to use pi-gpio on a raspberry pi 3 B and i’m getting this error: “gpio-admin: failed to change group ownership of /sys/devices/virtual/gpio/gpio22/direction: No such file or directory.” I’ve noticed on the package description on npm that the raspberry pi 3 B is not mentioned. Do you have any idea how to solve this issue?

        I’m trying to create a web application to control a car(2 motors and 3 servo motors + video streaming) using the raspberry pi model 3 B.

        If you have any other tips that I should take into consideration please let me know.

        Thank you!

        Best regards,
        Alex

        1. Hi Alex, I have not used the pi-gpio package, and I am not familiar with that error. You could open an issue on the pi-gpio GitHub page, or try a different npm package such as wiring-pi to accomplish your goals. Good luck!

  2. Thank you for a great tutorial.

    I made a project Home Automation. It works local network only by using lighttpd server. but web site not allowed to Internet.

    with this method u provided my website is on internet but, when i press buttons led not turned on, please help me

  3. Thank you for posting these!
    When I fire up lt – Im getting an error ‘Connection refused (check your firewall settings).

    Is there something that I’m missing or should I follow the comments above regarding nGrok.

    And if so – how do I uninstall lt?

    Thanks again!

    Oh – one last question. Is there a way to go thru these in order?

    1. Hi Jason, localtunnel was a great option when I originally wrote this article; however, it is no longer working reliably. Please follow my instructions in a previous comment on this page for ngrok instructions. I have plans to write an ngrok version of this article to replace this one.

      To uninstall localtunnel, issue the following command from the terminal:

      $ sudo npm uninstall -g localtunnel

      To follow my tutorials in order, you can go to my home page (https://thisdavej.com/) and the follow the links sequentially in the “Node.js IoT Tutorial Series” section. There are many more links on the home page that you can explore in any order according to your interests.

Leave a Reply

Your email address will not be published. Required fields are marked *