POST method endpoints

The POST method is typically used when we want to insert new data into our database.

POST /post. For this endpoint, we need to use Postman to be able to send the data through the body. To do this, you need to select the POST method in Postman. Use the URL http://localhost:3000/api/post, then click on Headers, and you need to add the header Content-Type with the value application/x-www-form-urlencoded:

After you set the header, then go to the Body tab and select the raw option, and you can send the information like this:

Now you can hit the Send button and see the response that the service returns:

If you did everything correctly, you should get a response with the saved node set to true and the post node containing information about the saved post. Now if you try to hit the Send button again with the same data (the same title), it will cause an error because, as you remember, our slug must be unique:

You are probably wondering what the __v is if we haven't added that node directly. That is the versionKey, which is a property set on each document when it's first created by Mongoose. This key's value contains the internal revision of the document. You can change or remove the name of this document property. The default is __v.

If you want to change it, you can do something like this when you are defining a new schema:

    // If you want to change the name of the versionKey
new Schema({...}, { versionKey: '_myVersion' });

Or if you want to remove it, you can pass false to the versionKey, but I don't recommend doing that because you won't have control on the version changes every time you update a document:

    // If you want to remove it you can do:
new Schema({...}, { versionKey: false });
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.227.194