NodeJS Tutorial Day 1

Learning Objective: Let’s explore NodeJS and Learn everything about NodeJS Programming, NodeJS App Development, and Deployment.


What is NodeJS?

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on a JavaScript Engine and executes JavaScript code outside a web browser. Source Wikipedia


How to Install NodeJS?

NodeJS Download Use this link to install NodeJS on the Latest operating system, If you are using older operating systems like Windows 7 then search like NodeJS for Windows 7


How to Check My Installation is Done or not?

Goto Command Prompt or Terminal and execute the following command, if you get some result on both commands your installation is perfect, if you get an error like command not found then your installation is not done properly.

node -v

npm -v


NodeJS Hello World Program.

Create Folder: C:\NodeJSProgram

Open Visual Studio Code and open this folder

Create a new File named: n1.js

write down the following code

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!');
}).listen(8080);

Open Terminal with Folder “C:\NodeJSProgram” and run the following command

node n1.js

Goto Browser and open the following URL

http://localhost:8080/

NodeJS Tutorial Day 2

Learning Objective: Let’s find rich Modules or Libraries that can help us to build NodeJS App easily. Here are the Node.js Built-in Modules.


For File Write/Read you need the following modules: (fs module)

var fs = require('fs');

fs.readFile('demofile.txt', 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

To display some data in the browser use the following modules: (http module)

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello World!');
  res.end();
}).listen(8080);

Want to read details about Operating System then use the following modules: (os module)

var os = require('os');
console.log("Platform: " + os.platform());
console.log("Architecture: " + os.arch());

Check out more NodeJS modules

Still looking for more modules here are 1000+ NodeJS Modules

NodeJS Tutorial Day 3

Learning Objective: Let’s play with some features of NodeJS to understand its structure and workings.

Creation of Function and display its output to Console

function sum(no1,no2){
    return (no1+no2);
}

console.log(sum(10,20));

Now let’s create our own Library / Module and make it into a separate file (I will create 2 files here, 1 file for the library and 1 file for calling the library)

My Library (mylib.js)

exports.sum = (no1,no2) => no1 + no2;

My File where I will import the above library and will use the library’s function

var mylib = require("./mylib");

console.log(mylib.sum(10,20));

Simple If conditions

no1=0;

if(no1>0)
{
    console.log("Positive");
}
else if(no1<0)
{
    console.log("Negative");
}
else
{
    console.log("Zero");
}

Simple Loop

for(i=1;i<10;i++)
{
    console.log(i);
}