How to create POST API in nodejs?

post api in nodejs

In this article, we will explore how you can create a post api in nodejs and save data in Mongoose.
we cover from scratch to develop post API in nodejs and you can follow our previous post for the good folder structure and MVC pattern and we will continue in that project.

Introduction

A POST request is one of the most commonly used methods in APIs. It is used when a client or user wants to submit data to a server. Unlike a GET request that retrieves data from a server, a POST request sends data to the server. The server processes the data and provides a response, sending the success or failure of the operation.

How Does a POST Request Work?

When a client initiates a POST request, it includes a payload or body containing the data to be sent to the server. This payload can be in many formats, such as JSON or XML. The data can represent various resources, such as user information, form submissions, or any other data that needs to be stored.

When to use a POST API in nodejs?

POST requests should be used when you need to send data to a server. Here are some common use cases for POST requests:

  1. Creating new resources: When you need to create a new resource on a server, such as a new user account or a new blog post, you can use a POST request to send the data to the server.
  2. Updating existing resources: If you need to update an existing resource on a server, such as updating a user’s profile or updating a blog post, you can use a POST request to send the updated data to the server.
  3. Submitting forms: When a user submits a form on a website, the data is sent to the server using a POST request.

What is Covered in the previous article?

In the previous article, we covered how to create folders for the project, follow the MVC pattern, initiate the project, and database connectivity, defined routes, created the controller and designed the schema, and performed the GET request you can read it from here we will proceed further.

Defining Routes for POST API in nodejs

Here we will define the routes for post requests you can open your UserRoutes folder and paste this code and follow the below screenshot.

router.post('/addUser', UserController.insertUser);
post api in nodejs


Defining the Controller

In this controller, you can import UserModel and paste this code or follow the below screenshot.

  async insertUser(req,res,next) {
        try {
            const request = await new UserModel(req.body).save();
            if (request)
                return res.json({ status: 200, data: request, message: 'User Saved' });
            else
                return res.json({ status: 404 });
        }
        catch (err) {
            console.log(err)
        }
    }
post api in nodejs

Hit the EndPoint:

Now everything is done let’s hit the endpoint

Your endpoint will be “​​http://localhost:4000/api/addUser”;

Now our data is saved in the database and we are getting 200 response code.

Conclusion – POST API in Nodejs

A POST request is a type of HTTP request that is used to send data to a server. When you make a POST request, the data is sent in the body of the request. POST requests are commonly used to create new resources on a server, update existing resources, or submit data to a server.

Read More
https://scribblersden.com/events-in-javascript/

Follow Us on
https://www.linkedin.com/company/scribblers-den/
https://www.facebook.com/scribblersden.blogs

Thank You

Related Post

Leave a Reply

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