Express.js API is a powerful tool for creating backend APIs for your web applications. In this tutorial, we will learn how to create a simple Express.js API with GET, POST, PUT, and DELETE methods in both Hindi and Urdu languages.
Step 1: Setting up Express.js and Nodemon
First, we need to install Express.js and Nodemon. Open your terminal and run the following commands:
npm install express
npm install nodemon --save-dev
Next, create a new file named app.js
and open it in your code editor.
Step 2: Creating a basic Express.js server
In your app.js
file, add the following code to create a simple Express.js server:
<!DOCTYPE html>
<html>
<body>
<h1>Express.js API Tutorial</h1>
<p>This is a tutorial on how to create a simple Express.js API with GET, POST, PUT, and DELETE methods.</p>
</body>
</html>
Step 3: Adding routes for GET method
Now, let’s add routes for the GET method. In Express.js, the GET method is used to retrieve data from the server. Add the following code to your app.js
file:
app.get('/', (req, res) => {
res.send('Hello, world!');
});
Step 4: Adding routes for POST method
Next, let’s add routes for the POST method. In Express.js, the POST method is used to send data to the server. Add the following code to your app.js
file:
app.post('/', (req, res) => {
res.send('Data received successfully!');
});
Step 5: Adding routes for PUT method
Now, let’s add routes for the PUT method. In Express.js, the PUT method is used to update existing data on the server. Add the following code to your app.js
file:
app.put('/', (req, res) => {
res.send('Data updated successfully!');
});
Step 6: Adding routes for DELETE method
Finally, let’s add routes for the DELETE method. In Express.js, the DELETE method is used to delete data from the server. Add the following code to your app.js
file:
app.delete('/', (req, res) => {
res.send('Data deleted successfully!');
});
Step 7: Running the server with Nodemon
To run your Express.js server with Nodemon, open your terminal and run the following command:
nodemon app.js
Your Express.js API server is now running on http://localhost:3000/
. You can test the endpoints using a tool like Postman.
That’s it! You have successfully created a basic Express.js API with GET, POST, PUT, and DELETE methods in both Hindi and Urdu languages. Feel free to customize and expand on this tutorial to create more complex APIs. Happy coding!