Getting Started With NodeJS On Termux

NodeJS on Termux: – NodeJS – an opensource runtime JavaScript environment, which can easily be installed and used on any type of Android device using an application called Termux. By writing this small article, I am trying my best to deliver you all the necessary and correct information that I have, about installing and using NodeJS on Android Device, Not only this, I will also guide you through the full process of writing your first JavaScript server program, and hosting your own server on your localhost – http://127.0.0.1/.

How To Install NodeJS on Termux?

Installing NodeJS on Termux, is a very easy task that even a non-termux user can easily understand the steps involved in this installation process. So please follow the below mentioned instruction carefully. And in a couple of moments, you’ll have your JavaScript system up and running.

1.Update your Termux packages:

pkg update

2. Upgrade your Termux packages:

pkg upgrade

3. Install NodeJS on your Termux:

pkg install nodejs

4. Check the version of node and verify the installation:

node -v

5. Check the version of npm – the Node Package Manager:

npm -v

6. How to use npm? Check npm help menu:

npm --h

How To Run NodeJS on  Termux?

Before running NodeJS, you need to create a JavaScript file first, and then you need to write some JavaScript codes. So use the following Termux commands to create your first JavaScript file on Termux:

nano hello.js

Here is an example of a simple JavaScript program, copy paste them into the server.js file

// the hello world program 
console.log('Hello World');

 

After writing the program, you can run the script by using:

node server.js

In the above command, server is the filename and ‘.js’ is the extension for a JavaScript file.

How To Host NodeJS Server on Localhost using  Termux?

 

 

Once you create your website, or design your website using HTML & CSS, you can use NodeJS to connect it with your localhost server.

But to host it on your localhost, you need to install another server package called “http-server”. This simple server package will host your website on your localhost. To install this package you need to use the following npm command on your termux:

npm install -g http-server

Remember, the name of your HTML file must be ‘index.html’ to host using this package.

How To Host NodeJS Server on Localhost using  Termux?

Down below, I have given a simple example of how you can host a HTML website on your localhost using NodeJS & Termux.

Let’s create a simple JavaScript program:

1. Create a JS file that will connect our html script with the server. Use the following nano command to create the file:

nano server.js

2. Now paste following JavaScript program in the ‘server.js’ file:

Javascript:

const http = require('http'); 
const fs = require('fs'); 

const server = http.createServer((req, res) => {
const htmlFile = fs.readFileSync('index.html', 'utf-8'); 
res.writeHead(200, { 'Content-Type': 'text/html' }); 
res.end(htmlFile); }); 

server.listen(3000, '127.0.0.1'); 
console.log('Server running at http://127.0.0.1:3000/');

 

How To Host Your Website on Localhost using  NodeJS & Termux?

3. Create an HTML program to design our Website. The name for this file should be index.html. Use  the command:

nano index.html

4. Now, paste the following simple html code in the index.html file:

HTML


<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>Node.js on Termux</title> 
</head> 
<body> 
<h1>Hello from Node.js on Termux!</h1> 
</body> 
</html>

5. Now once you complete all the previous steps, you are ready to host your website on your localhost. Hit the following command to do it.

node server.js

This command will quickly start your server on your localhost (http://127.0.0.1/) & obviously it will show the port on your termux, and in most of the cases, it is 3000.

6. Now to access your website, open a browser like Chrome, Brave, Opera etc, and enter the following credential on the search box:

http://localhost:3000

Conclusion:

NodeJS is a good choice for you, that you’ve chosen to start your web development journey. I hope this simple article could help you begin your new life in web development on your android device. If you have any question or query related to this, you can leave us a comment down below.

6 thoughts on “Getting Started With NodeJS On Termux”

  1. Thanks for your marvelous posting! I seriously enjoyed
    reading it, you could be a great author. I will ensure that I bookmark your blog and will come back
    very soon. I want to encourage you to definitely continue your
    great posts, have a nice afternoon!

    Reply
  2. Excellent post. I used to be checking continuously this weblog and I’m impressed!
    Very useful information specially the ultimate section 🙂 I
    care for such information a lot. I was looking for this particular info for a very long time.

    Thanks and best of luck.

    Reply

Leave a Comment