How To Create Node.js Apps On Android? : Termux.

Node.js is a JavaScript runtime environment that allows you to build scalable and performant server-side applications.

What Is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment that executes JavaScript code outside of a web browser. It is built on Chrome’s V8 JavaScript engine and allows developers to build scalable and performant server-side and networking applications.

Node.js is a popular choice for developing web applications because it is fast, efficient, and easy to learn. It is also a good choice for developing real-time applications, such as chat applications and multiplayer games.

What Is NPM?

npm is a package manager for Node.js packages, also known as modules. It is the default package manager for the Node.js runtime environment. npm allows developers to easily install, manage, and update Node.js packages from a public registry or from private registries.

Node.js is available for almost every devices including Android via Termux. In this blog post we will see how to Install Node.js, Install and use Node.js packages, Create and run Node.js Applications.

Installation

To install Node.js on Android first install Termux from F-Droid or Github.

Ryn the following commands to install.

Update Termux Repositary

pkg update && pkg upgrade
#Or
pkg upd; pkg upg

Install nodejs and npm

pkg install nodejs npm
#Or
pkg i nodejs npm

For other Platforms.

For other platforms such as Windows, MacOs refer official node.js page.

After installation is completed check the version of both node.js and  npm.

node --version

npm --version

Simple Hello World App In Node.js

1. Create App Directory.

Create a new directory for your project and navigate to it in your terminal. Here I created helloworld.

mkdir helloworld && cd helloworld

2. Initialize New Node.js Project.

Initialize a new Node.js project by running the following command:

npm init -y

3. Create Node.js App File.

Create a new file called index.js and write the following code:

var msg = "Hello, World!"

console.log(msg)

4. Run The Node.js App

node index.js

Hello World Server App in Node.js

1. Create App Directory.

Create a new directory for your project and navigate to it in your terminal. Here I created myServer.

mkdir myServer && cd myServer

2. Initialize New Node.js Project.

Initialize a new Node.js project by running the following command:

npm init -y

3. Create Node.js App File.

Create a new file called index.js and write the following code:

const http = require('http');

const server = http.createServer((req,  res) => {
    res.writeHead(200,{'Content-Type': 'text/plain'});
    res.end('Hello, World!');
});

server.listen(3000, () => {
    console.log('Server listening at http://localhost:3000');
});

4. Run The Node.js App

node index.js

Hello World Server App in Node.js Using Express

1. Create App Directory.

Create a new directory for your project and navigate to it in your terminal. Here I created myExpress.

mkdir myExpress && cd myExpress

2. Initialize New Node.js Project.

Initialize a new Node.js project by running the following command:

npm init -y

3. Install And Add Express Module Using NPM

Run the following command to install express module. Thit will add express module in node_modules directory of current project.

npm install --save express

4. Create Node.js App File.

Create a new file called app.js and write the following code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('<a href="/about">Click me</a>');
});

app.get('/about', (req, res) => {
    res.send('<h2>Hello, World! Nodejs App Using Express By Easymux</h2>')
});

app.listen(port, () => {
    console.log('Server listening at http://localhost:'+port)
})

5. Run The Node.js App

node app.js

Conclusion

Using Node.js on Mobile device might be limitations regarding performance, resource availability, or device-specific constraints compared to using Node.js on traditional desktop or server environments. Yet, for learning, prototyping, or specific mobile-focused projects, using Node.js on Android via Termux can be a valuable and convenient option.

Deals Of The Day

Discover more from Easymux

Subscribe now to keep reading and get access to the full archive.

Continue reading