What is node js | Node.js as a Web[HTTP]Server | Sample REST API Creating using Node.js Only.

Node js tutorial

What is node js | Node.js as a Web[HTTP]Server | Sample REST API Creating using Node.js Only.

Share this article:

Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp

BACKEND Technologies:
---------------------------
i)PHP + MYSQL
ii)ASP.NET +SQL SERVER
iii)JSP + ORACLE

iv)NODE.JS + EXPRESS + MongoDB

What is Node.js ?

Node.js is a javascript based server side platform which let
Javascript to be executed outside of the WebBrowser.

It is an virtual server runtime which expects HttpRequest and returns
HttpResponses that can  be HTML,Raw String or JSON.

Node.js is based on Asynchronous approach.
i.e it can handle multiple users at the same time.

Node.js makes the application more optimized & faster in response.


Node.js   = JavaScript Library + Runtime Environment[Platform]
           document,window,console

How to get started with Node.js :
-----------------------------------------
https://nodejs.org/dist/v18.13.0/node-v18.13.0-x64.msi


After installation, Please check or verify the version of Node.js


open cmd and type the following command

node --version   ==>Node.js Version or node --v

npm  --version  ==>Node Package Manager , node based plugins we can install uninstall.


How to run Node.js Application ?

Open Up Terminal make sure it is selected to cmd not powershell,
node <filename.js>
i.e
node hello.js

Hello Node

----------------------------------------------------------------------


Node.js as an Web[HTTP]Server:

Node.js is consisiting of lots of built in modules like
http,url, and many more.

If we want to use Node.js as a WebServer we have to use http module
which is a built in module.


==========================
Sample Code:
==========================

//loading the http module
const http = require('http');

//Specifying HTTP Listener PORT
const port = 3000;

//create a virtual server using http module
http.createServer((req,res)=>{
    //Setting up the response Type....
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write("<h1>Hello, Welcome to Node.js</h1>");
    res.write("<p>Hello World</p>");
    res.end('');
}).listen(port);

console.log(`Server has started at ${port}`);


Run : node server.js

Then Open Up any Browser and Type
http://localhost:3000/

or
localhost:3000/

PORT Number can be given any 4 digited Number apart from [8080]






Sample REST API Creating using Node.js Only.
-------------------------------------------------

//loading the http module
const http = require('http');

//definfing the port number
const port = 3000;

var foodList=[
    {'food_id':1001,'food':'Cicken Biriyani','price':200},
    {'food_id':1002,'food':'Mutton Biriyani','price':350},
    {'food_id':1003,'food':'Chicken Chaap','price':120},
    {'food_id':1004,'food':'FriedRice','price':180},
    {'food_id':1005,'food':'Hakka Noodles','price':100}
];//Object Typed Array
//create a virtual server using http module...
http.createServer((req,res)=>{
    //Type of response.
    //res.writeHead(200,{'Content-Type':'application/json'});
    if(req.url=='/'){
       res.setHeader('Content-Type','text/html');
       res.write("<h1>Welcome to REST FOOD API</h1>");
       res.write("<a href='/api/foods'>See ALL</a>");
       res.end('');  
    }else if(req.url=='/api/foods'){
       res.setHeader('Content-Type','application/json');
       res.write(JSON.stringify(foodList));
       res.end('');
    }
   
   
}).listen(port);

console.log(`Server has started at ${port}`);


Share this article:

Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp