Install Node js in Termux | Android

What is Node Js ?

Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. Historically, JavaScript was used primarily for client-side scripting, in which scripts written in JavaScript are embedded in a webpage's HTML and run client-side by a JavaScript engine in the user's web browser. Node.js lets developers use JavaScript to write Command Line tools and for server-side scripting—running scripts server-side to produce dynamic web page content beforethe page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifyingweb application development around a single programming language, rather than different languages for server side and client side scripts.


How to install node js in Android ?

Open termux app and type following commands :

$ pkg update
$ pkg install nodejs
$ pkg install nano

How to install node js in Ubuntu ?

Open terminal and type following commands :

$ sudo apt-get update
$ sudo apt-get install nodejs

To write your node js program type:

$ nano file_name.js

Hello world program in node js :

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

To run node.js script type :

Open termux app or terminal and type following commands :

$ node filename.js

Then open browser and type :

http://localhost:8080

Practical Video :



Comments

  1. how to install npm for me to install package like express and mongoose

    ReplyDelete

Post a Comment