Building A Node - Js Application On Android - Part 1:termux, Vim Andnode - Js
Building A Node - Js Application On Android - Part 1:termux, Vim Andnode - Js
js
application on Android -
Part 1:Termux, Vim
andNode.js
If you are excited about Node.js and own an Android device, no doubt you’ll enjoy running Node.js
on it. Thanks to Termux a terminal emulator and Linux environment for Android, the fun of
developping Node.js web applications is just a few ‘npm install’s away!
1. Termux
Termux combines terminal emulation with a Linux package collection. It comes as a free app that
can be installed directly from the Play Store or from F-Droid catalogue.
Configuration
When you open Termux, you are greeted by a Command Line Interface (CLI). Right after installing
Termux, it is recommended to check for updates, and upgrade if need be. So type the following
commands at the prompt — that is, after the ‘$’ sign — and press <Enter>:
Termux comes with a minimal base system, so you should also install ‘coreutils’ for the full-fledged
variants of base CLI utilities such as ‘mv’, ‘ls’, etc.
$ apt install coreutils
Storage
There are three main types of storage in Termux:
1.App-private storage: This is right where you are when
you start Termux.
Although the environment setup in Termux is similar to that of a modern Linux distribution,
running on Android implies differences and so far I have only managed to run Node.js fully while
storing my data in Termux’s private storage (option 1 above).
So let’s create a directory for our app and change to this directory:
Keyboard
I have only been using a soft keyboard so far and I encountered some issues with the default touch
keyboard while using the volume up key as a replacement for <Esc>, <Tab> or the Arrow keys.
To circumvent these issues, I installed Hacker’s Keyboard from the Play Store and I really like it. It
is a touch keyboard that can be used instead of the default one and has all the keys needed for
writing code and using the terminal.
You can find useful information about using a touch or hardware keyboard with Termux directly
on the Help page.
2. Vim
Vim is a text editor that can be used right in the Command Line Interface and it is available as a
package in Termux. So let’s install it:
Vim’s interface is not based on menus or icons but on commands given in a text user interface. In
case you are new to it I’m going to guide you through the very basics of Vim.
$ touch server.js
$ vim server.js
Tilde lines are here to indicate that these lines are not part of the content of the file.
To start writing into the file, you need to switch to writing mode. So just type the letter “i”. At the
very bottom, you should now see something like this:
mode
2.Type :wq and press <Enter> to save the changes and quit.
3.Type
:q! and press <Enter> to quit without saving the
changes.
Something like this simple Vim Reference might be useful if you are new to Vim. Alternatively, you
can type ‘vimtutor’ in the terminal for a 30 minutes tutorial, play a learning game at http://vim-
adventures.com/ or follow the interactive tutorial at http://www.openvim.com/.
3. Node.js
Installing Node.js is very simple:
If you haven’t done it yet, create a folder for the application, move into it and type:
$ npm init
This will ask you a bunch of questions, and then write a ‘package.json’ file for you. (You can just
press <Enter> for each question asked.)
Now let us check that everything is working all right. Open server.js
$ vim server.js
and type in it
console.log('This is Node.js running on Android.')
$ node server.js
This should print the text “This is Node.js running on Android.” in the terminal.
In a nutshell
As a recap, here is the whole process again (with minor differences as it is all done directly from the
command line).
Wrapping it up
We have seen how to use Termux on Android, how to edit files with Vim and how to run Node.js.
Here are a the main links related to Termux: its web page, its wiki and its GitHub repositories. It can
be installed from the Play Store or from the F-Droid catalogue.
In the next post we are going to build a basic Node.js application using the Express web framework
and a lightweight JavaScript database called NeDBwhich uses MongoDB’s API and can be used to
develop and run a web application in Termux.