Prologue

I have found myself lately doing a lot of proof-of-concept applications in NodeJS using a simple web interface. And running them whenever and wherever I want seemed like a very nice idea. I could have chosen to have them hosted on a server, however I chose to run them on my Android phone because:

Sneak peek

NodeJS application on Android sneak peek

What you need

Suggestions

Transferring files between your PC and your phone may be required in this guide. A file transfer solution that can help keep things organized and also works wirelessly would be to use a FTP server application on the phone such as this one, then connect to it using a FTP client application on your PC, such as this one. If you are not interested in such a solution, skip to “The steps” ahead.

FTP server application on Android screenshot
Filezilla Windows application screenshot

The steps

Set up the environment in Android

We need the Termux application on Android. Quoting them:

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required.

In other words, Termux allows you to run various Linux apps and also offers a terminal interface for the environment. In our case, it will allow us to run Node inside it.

Installing Termux

Termux Android application screenshot

Installing NodeJS inside Termux

pkg upgrade
pkg install nodejs
Termux screenshot for pkg install nodejs

Testing NodeJS application inside Termux

termux-setup-storage
cd /sdcard
ls
Termux list of directories screenshot
cd /sdcard/path/to/app
node index
NodeJS application running on Android screenshot

Making it easy to work with Termux

Typing a lot in Termux is annoying. Luckily, there is a solution: install a SSH server on Termux and connect to it from your PC. You will have a remote terminal in which you can type your commands right from your PC. The steps in this section are not required, they are just a recommendation.

pkg install openssh
passwd
Termux passwd command screenshot
whoami
Termux whoami command screenshot
sshd
mRemoteNG Windows application screenshot
mRemoteNG example

Create a launcher for the NodeJS application

All is fine for now. But it is very cumbersome to always need to manually start the Node application and then the browser. We will fix this by creating a bash script.

cd ~
mkdir .shortcuts
chmod 700 -R .shortcuts
nano launcher.sh
cd /sdcard/path/to/your/application
node index & :
sleep 5
termux-open-url http://localhost:8080
wait $!

This script will launch your application, wait for 5 seconds for the application to load, then launches your phone browser pointing it to the application’s URL. The last line keeps the Termux process open so that your application will be kept running in the background.

nano application screenshot
./launcher.sh

After a short while your phone browser should open your Node app’s web interface.

NodeJS application web interface Android screenshot

You can shutdown the Node application by tapping on Exit on the Termux notification on Android or by pressing CTRL+C on the terminal that launched the application, be it in Android inside Termux or on a remote terminal inside a SSH client.

Launch your NodeJS application from the phone’s home screen

Until now you could run your application by opening Termux and typing somethink like

cd ~/.shortcuts
./launcher.sh

But we can make it even easier to launch the Node application, right from the phone’s home screen:

Termux:Widget searches for scripts inside the ~/.shortcuts directory and creates a widget that enables you to run the scripts with one tap.

Termux Android widget screenshot

Don’t forget that you can shutdown the NodeJS application by tapping Exit on the Termux notification or by switching to Termux on the phone and sending CTRL+C.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.