Creating REST API | Simplified
What is an API? An API is an Application Programming Interface that lets you connect two computers, where one computer or application requests the data from the server.
What is a REST API? A REST API or RESTful API is an application programming interface that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.
Re- Representational S- State T- Transfer
Steps:
-
Install Node.js on your machine, using the following link https://nodejs.org/en/
-
create an empty directory and initialize your project by running the following command
mkdir your-folder-namecd your-folder-namenpm init -y
-
The next step is to create an empty JavaScript file and install Express.
npm install express
-
Initialize the
appand start the local server at port 3000.Run
node index.jsto start the server. -
Now the server is running successfully at port 3000, Next we create an
endpoint.The
getmethod allows us to create HTTP GET requests.It accepts two params: - The first is path/route. - The second is a callback function that handles the request to the specified route. -
Suppose we want to display all the users whenever the client requests the "/users" endpoint.
We have the
sendmethod, to return the data from the server to the clientWe will send an array of objects with
nameandidfields. -
Run node index.js again and go to the path specified in the code to check the APIs in action! 👍