Node.js is a JavaScript runtime environment that allows you to build scalable and performant server-side applications.
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.
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.
To install Node.js on Android first install Termux from F-Droid or Github.
Ryn the following commands to install.
pkg update && pkg upgrade
#Or
pkg upd; pkg upg
pkg install nodejs npm
#Or
pkg i nodejs npm
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
Create a new directory for your project and navigate to it in your terminal. Here I created helloworld.
mkdir helloworld && cd helloworld
Initialize a new Node.js project by running the following command:
npm init -y
Create a new file called index.js and write the following code:
var msg = "Hello, World!"
console.log(msg)
node index.js
Create a new directory for your project and navigate to it in your terminal. Here I created myServer.
mkdir myServer && cd myServer
Initialize a new Node.js project by running the following command:
npm init -y
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');
});
node index.js
Create a new directory for your project and navigate to it in your terminal. Here I created myExpress.
mkdir myExpress && cd myExpress
Initialize a new Node.js project by running the following command:
npm init -y
Run the following command to install express module. Thit will add express module in node_modules directory of current project.
npm install --save express
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)
})
node app.js
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.