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
- First Using Linux Batch Code navigate to the Files that your project reside
- Type ” npm init “
- and Wait for the process to run
- After you have done , you will see a “node_modules” folder appear in your Project Folder
Import the Express Folder
- Type this batch code ” npm install –save express”
- Wait for the System to run
Now is time to create your Web Request API Lets Code
- Well First the API need to reach the File “express ” by using the require() Function
- Data are assign into the express variable.
- 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'); });
- 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
- 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
- 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:
- 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
- call the Program type ” node yourProgramName.js ” at the batch command tool
Send Request through Web Browser
- 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