Create API In Nodejs

Updated: Mar 8



An API provides interactions between one software and another, but not users.


What is REST?

Application programming interfaces (APIs) are everywhere.

They enable software to communicate with other pieces of software internal or external.

REST is an acronym for Representational State Transfer. It is web standards architecture and HTTP Protocol.

A REST Server simply provides access to resources and REST client accesses and modifies the resources using HTTP protocol.

REST was first introduced by Roy Fielding in 2000.

REST quite common nowadays for online services to have public-facing APIs.

These enable other developers to easily integrate features like social media logins, credit card payments, and behavior tracking.


REST API Operations


HTTP Verbs describe the type of operation:

  • GET: Retrieve a resource

  • POST: Create a resource

  • PUT: Update a resource

  • DELETE: Delete a resource


Advantages of REST APIs


  • Rapid API is a platform for accessing web-based REST APIs. As defined previously, APIs connect services. In addition to services, APIs allow an application to access external data.

  • With the help of REST API, one also will be able to organize complicated applications into a very easy to use resource

  • REST API is cleaner and very easy to explore and discover.


API Code Tree Structure






How to write code

index.js

const http = require('http');
const fs = require("fs");
const server = http.createServer(function(req,res){
 if(req.url =="/"){
 res.end("hello from home sides");
    }
 else if(req.url =="/userapi")
    {
 fs.readFile(`${__dirname}/userAPI/userapi.json`,"utf-8", (err,data) =>{ 
   console.log(data);
   res.end(data);
        });
server.listen(3000, "127.0.0.1",() =>{
 console.log("listening to the port no 3000");
});

userapi.js

[{
 "id":1,
 "name":"rk singh",
 "age":25,
 "email":"rk123@gamil.com",
 "city":"bhind"

},
{
 "id":2,
 "name":"bk jha",
 "age":24,
 "email":"bk123@gamil.com",
 "city":"noida"
 
 },
 {
 "id":3,
 "name":"aj kumar",
 "age":26,
 "email":"aj123@gamil.com",
 "city":"agra"
 
 },
 {
 "id":4,
 "name":"bk garg",
 "age":28,
 "email":"garg123@gamil.com",
 "city":"bhind"
 
 }
]


How to run it


PS C:\node js> cd .\httpserver\

PS C:\node js\httpserver> node index.js


Output




















Contact us for this nodejs assignment Solutions by Codersarts Specialist who can help you mentor and guide for such nodejs assignments.


If you have project or assignment files, You can send at contact@codersarts.com directly or contact at


Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

Terms and Conditions        Privacy Policy    Pricing Policy       Refund Policy         COPYRIGHT © 2021

Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram