Create Your First API

Create Your First API

Create Your First API , In this Article , i will show you how to build your very First API using NodeJS.

So What is an API ?

An API is an interface or messenger which takes the Request from the User and tell the System whats the User wants the System to Do, and return the Response from the System back to the User.

What Do we Need to build this API :

  • NodeJS
  • Batch Script
  • NPM

What is Node Js ?

Node  is just a JavaScript Code run time running  in a VA Machine.VA Engine is an Opensource Javascript Engine written in C++ that accept  JavaScript code and compile it into machine code

What is NPM ?

NPM is an Opensource platform where Developer upload their Node Package which can be used by other developer . It is also an online Command Tool Platform which User can install and un install package , changing Version control etc.

Setting Up The Node Module Package

  1. First Using Linux Batch Code navigate to the Files that your project reside
  2. Type ” npm init “
  3. and Wait for the process to run
  4. After you have done , you will see a “node_modules” folder appear in your Project Folder

Import  the Express Folder

  1. Type this batch code ” npm install –save express”
  2. Wait for the System to run

 

Now is time to create your Web Request API Lets Code

  1. Well First the API  need to reach the File  “express ” by using the require() Function
  2. Data are assign into the express variable.
  3. Then the express Function is assign to the ” app” Variable

The Request Message Code Below shows

 

app.get('/',function(request,response){

response.send('Hey i am Listening');
});


  1. This Mean, ” when the User input “/” into the listened Port , hey System go send my request , and the user demand back a response from the User
  2. So When the User input the / and the system will send the request through JSON and return the Response “‘Hey i am Listening’”

The Request Message Code Below shows

 

app.get('/',function(request,response){

response.send('Hey i am Listening');
});


The Listen Message Code Below shows

  1. In order the User to send a request , you must tell the System which port the System should listen to in this case is Port “3000:
  2. So you call the function and the System listening on Port 3000

app.listen(3000,function(){
    
    console.log(" Running on port 3000");
});

After you done the Program

  1. call the Program type ” node yourProgramName.js ” at the batch command tool

Send Request through Web Browser

  1. Go to the Web Browser and type ” localhost:3000/” to send the request

Check out how to develop a simple Android App Herehere

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × 1 =