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:
- I don’t need to manage a server
- I don’t need to pay for a server
- I don’t run them so frequently that they would justify using a server
Sneak peek
What you need
- You need an Android phone that can run the Termux application
- A PC would also be of a great help, but you can start running Node applications on your phone without one if you can get the application files on your phone
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.
- Install the FTP server app on your phone and start it.
- Install the FTP client app on your PC, start it, then connect it to the FTP server on the phone by filling in connection data that are displayed on the phone app:
- Fill the “Host” with the part between “ftp://” and “:” on the phone
- The “username” and “password” fields with the username and password that are displayed on the phone
- The port field with the part after the “:” on the ftp row on the phone
- Click on “Quickconnect”
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
- Download the latest Termux APK from this page.
- Transfer the APK to your phone.
- Install the APK
- Find and run Termux on your phone to make sure everything is ok. You should get a screen like this:
Installing NodeJS inside Termux
- Type the following commands in Termux. Run pkg upgrade to get everything up to date inside Termux, then run pkg install nodejs to Install Node:
pkg upgrade
pkg install nodejs
Testing NodeJS application inside Termux
- For Termux to be able to access files from the phone’s storage (and not only from the own data folder of Termux), run:
termux-setup-storage
- Accept the permission request.
- Test that you have successful access to storage from Termux by typing:
cd /sdcard
ls
- You should get a list of the directories in the phone storage root.
- Get your Node application files on the phone.
Here is a Hello, world application that creates a simple web server which displays “Hello, World!”:
- Navigate to the Node application’s directory, then execute Node on your main application file:
cd /sdcard/path/to/app
node index
- If the application has created a web server, open the phone’s browser and enter the application’s URL (something similar to “localhost:8080”).
The application should also be accessible from the local network if the phone is connected to WiFi. On another device connected to the network, enter the phone’s IP address in the browser, followed by the port the application listens on, if necessary.
Tip: If you used the FTP server application on your phone as described in the “Suggestions” section, you can easily find out the phone’s IP address from the application’s main screen. - To close the application, send CTRL+C to termux by tapping on “CTRL” on the screen, then entering C from the phone’s virtual keyboard.
- Alternately you can close Termux altogether by going to the phone’s notification area and tapping “Exit” on Termux’ notification
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.
- Install OpenSSH by typing:
pkg install openssh
- Create a password for your Termux user. Type “passwd”, enter, then your desired password two times. Note that there will be nothing displayed while you type your password.
passwd
- Find out your Termux username by typing:
whoami
- Type the following to start the OpenSSH server:
sshd
- On your PC, install a SSH client, such as PuTTY or mRemoteNG
- PuTTY is very lightweight
- mRemoteNG supports more protocols and you can open connections in multiple tabs
- Use the following connection parameters in the SSH client:
- Protocol: SSH version 2
- Hostname: your phone’s IP address (Note: it can be easily obtained from the FTP server application from the “Suggestions” section)
- Port: 8022
- Username: the username found with the whoami command in Termux
- Password: the password that was set with the passwd command in Termux
- For the rest of the guide you can use your SSH client to send commands to Termux
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.
- Create a “.shortcuts” directory in Termux’s home directory and give it read, write and executable permissions
cd ~
mkdir .shortcuts
chmod 700 -R .shortcuts
- Start editing a new file called “launcher.sh” in the “.shortcuts” directory.
nano launcher.sh
- Type the following, while replacing the directory, main Node application file name and the application listening port with the actual ones.
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.
- Save your file and close nano by pressing CTRL+O, Enter, CTRL+X
- Test your script by typing:
./launcher.sh
After a short while your phone browser should open your Node app’s web interface.
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:
- Download the latest APK of Termux:Widget from here.
- Transfer the APK file on your phone and install it.
Termux:Widget searches for scripts inside the ~/.shortcuts directory and creates a widget that enables you to run the scripts with one tap.
- Long press your phone’s home screen, access the “add widget” functionality and look for the Termux widget.
- After adding the widget you should have something like this
- Tap the “launcher.sh” entry to launch your NodeJS application.
- …aaand you’re done! :)
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.