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

web server imageIn 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

Right click on Windows folder and open with Visual Studio Code

I’m all about using Visual Studio Code for Node.js development.  It is a lightweight code editor and runs on Windows, Mac OS X, and Linux.  I enjoy it so much that I also use it for Node.js projects on the Raspberry Pi.  To accomplish this, I create a Windows file share on the RasPi as described in my Beginner’s Guide to Installing Node.js on the Raspberry Pi and map a drive on my Windows system to the RasPi.  I am then able to use VS Code to create my Node.js code and jump onto the RasPi to run the actual code.

Anyway, back to our regularly scheduled program…  Today’s topic is aimed at Windows users who are using Visual Studio Code and want to be able to right click on a given folder and launch VS Code.  We’re going to add a right click context menu item to “Open Folder as VS Code Project” since it saves time—and it’s more fun! Continue reading

Share

Create a Web Server in Node without any Code

Learning through Making (LTM) logoWelcome back!  In a previous tutorial (Beginners Guide to Installing Node.js on a Raspberry Pi), we installed Node.js on a Raspberry Pi. We are now ready to continue our LTM (Learning through Making) tutorial series and build a simple web server without any code.  While this tutorial is geared toward the RasPi, the instructions can be easily mapped over to other platforms such as Windows, Linux, and OS X.  Let’s get started! Continue reading

Share

Node Newbie Error – NPM Refusing to Install Package as a Dependency of Itself

I encountered an error and thought I’d post the solution here since it just might happen to you sometime!  Here’s the scenario:

I was planning to take the winston npm module out for a spin to try out it’s awesome logging capabilities.  I created a directory called winston to create a project and conduct the test.  What would be more logical than that?  🙂

Next, I ran npm init to create a package.json file hitting my Enter key as fast as it could go to accept all the defaults for this throwaway test project:

npm init screencast gif

Continue reading

Share

Learning through Making – Getting Started with Node.js

Learning through Making (LTM) logoToday we are kicking off our LTM (Learning through Making) series of tutorials to delve into the amazing world of Node.js.  We’re going to do some fun projects along the way, but we first need to lay a foundation and get Node.js installed.

What is Node.js?

Node.js logoAs described on Wikipedia, “Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications.” Node.js is awesome because it allows us to build a gamut of applications from highly scalable networked applications to console applications.  Node.js uses a single-threaded event loop model to process events in an event queue which helps it process high amounts of I/O.  This differs from say, the Apache web server, which uses a thread for each new connection and can thus not handle so many concurrent connections.  Node.js is based on JavaScript and is further positioning JavaScript as a universal language that every developer should know. Let’s be on a first name basis with “Node.js” and simply call it “Node” from here on out. Continue reading

Share