Create API in NodeJs

 API

  • API means Application programming interface.
  • API is the acronym for application Programming interface, which is a software intermediary that allows two applications to talk to each other. Each time you use an app like facebook, send an instant message or check the weather on your phone, you're using an API.
  • in simple way API is a like a service which allow to request data.
  • else if(req.url == "/userapi"){
    fs.readFile(`${__dirname}/Userapi/userapi.json`,"utf-8",(err,data)=>{
    console.log(data);
    res.end(data);
    }

const fs = require("fs");
const http = require("http");
const server = http.createServer((req, res)=>{
    if(req.url == "/"){
        res.end("hello, this is home page");
    }else
    if(req.url == "/data"){
        fs.readFile("./createApi.json", "utf-8", (err, data)=>{
            res.end(data);
        })
    }else{
        res.end("404 error, page does not exit");
    }
});

server.listen(8000, "127.0.0.1", ()=>{
    console.log("listening...");
})
Output
PS F:\nodejs> node app.js
listening...