20
Node CRUD Operations
CRUD (create, read, update, delete) is an acronym that refers to the four functions. There are different requests for each issue. For querying, we have GET requests, for sending data we have POST requests. These are called HTTP requests. They enable interactions between client and the server and work as a request-response protocol.
The HTTP requests are:
GET is used to request data from a specified resource.
POST is used to send data to a server to create/update a resource.
HEAD: Same as GET, but it transfers the status line and the header section only.
PUT: Replaces all the current representations of the target resource with the uploaded content.
DELETE: Removes all the current representations of the target resource given by URI.
CONNECT: Establishes a tunnel to the server identified by a given URI.
PATCH: The PATCH method applies partial modifications to a resource
Route definition takes the following structure:
app.METHOD(PATH, HANDLER)
Where:
-
app
is an instance of express. -
METHOD
is an HTTP request method, in lowercase. -
PATH
is a path on the server. (URL path) -
HANDLER
is the function executed when the route is matched. (Handler function)
For GET method:
app.get('/save', function(req, res) {
// write query here
});
For POST method:
app.post('/save', function(req, res) {
// write query here
});
For PUT method:
app.put('/save/:id', function(req, res) {
// write query here
});
For DELETE method:
app.delete('/save/:id', function(req, res) {
// write query here
});
20