DEV Community

Md Riyajul Kabir
Md Riyajul Kabir

Posted on

NodeJs CURD Operation

CRUD Operations
CRUD is an acronym for Create, Read, Update, and Delete. These are the four basic functions that can be performed with most traditional database systems and they are the backbone for interacting with any database.
The method is the type of request you send to the server.
GET
POST
PUT
PATCH
DELETE
Post Method
Use the post method to create any new data. When creating a new resource, POST the parent and the service takes care of adding new resources to the parent, assigning an ID (new resource URI), etc.
If created successfully, return HTTP status 201, 201 Return a location header with a link to the newly created resource with HTTP status.
// Create a new Note
app.post('/notes', notes.create);

Get Method
Use the get method to get all data or a single data by its id. The HTTP GET method is used to "read" or retrieve an asset presentation. In the "happy" or non-error path, GET provides a presentation in XML or JSON and an HTTP response code of 200 (OK). In case of an error, it often returns a 404 (not found) or 400 (bad request).
// Retrieve a single Note with noteId
app.get('/notes/:noteId', notes.findOne);

// Update a Note with noteId
app.put('/notes/:noteId', notes.update);

Put Method
Use the put method to update or edit data.if a PUT call on an asset increases one counter of the asset, the call is no longer invincible. Sometimes this happens and it may be enough to document that the call is not indomitable. However, it is advisable to keep PUT requests in abeyance. It is strongly recommended to use POST for non-refractory requests.
// Update a Note with noteId
app.put('/notes/:noteId', notes.update);

// Update a Note with noteId
app.put('/notes/:noteId', notes.update);

Delete Method
Use the DELETE method to get all data or a single data by its id.If successfully deleted, return HTTP status 200 (OK) with a response body, perhaps a presentation of the deleted item (often demanding too much bandwidth), or a wrapping response (see return value below). Either that or return HTTP status 204 (no content) without a response body. In other words, a 204 status without any body, or JSEND-style response and HTTP status 200 is the recommended response.
// Delete a Note with noteId
app.delete('/notes/:noteId', notes.delete);

Top comments (1)

Collapse
 
drarig29 profile image
Corentin Girard

You have a typo in the title 😊