Back to Blog
Lesson 19 of the Node.js: Build Your First Server & CLI course
Node.jsAugust 6, 20264 min read

Handling HTTP Methods: Building CRUD Routes in Express

Master HTTP methods in Express. Learn to implement POST, PUT, PATCH, and DELETE routes to create a fully functional, CRUD-capable REST API.

Node.jsExpressRESTHTTPBackend
Smiling delivery man holding packages inside a building, ready for delivery.

Previously in this course, we covered initializing an Express app and defining basic routes. While GET allows us to fetch information, a professional server must also allow users to modify, create, and remove data.

In this lesson, we move beyond simple data retrieval to implement the full suite of HTTP methods required for a functional REST API.

Understanding HTTP Methods in REST

In RESTful architecture, the HTTP verb (the "method") tells the server what action to perform on a resource. We map these to CRUD (Create, Read, Update, Delete) operations:

MethodCRUD OperationPurpose
GETReadFetch a resource
POSTCreateSubmit new data to be processed
PUTUpdateReplace an entire resource
PATCHUpdateModify a specific part of a resource
DELETEDeleteRemove a resource

Handling POST Requests

POST is used to send data to the server to create a new resource. Unlike GET, which puts data in the URL, POST carries data in the request body. In Express, you define a POST route just like a GET route, but using app.post().

JAVASCRIPT
app.post(CE9178">'/tasks', (req, res) => {
  // Logic to save a new task would go here
  res.status(201).send(CE9178">'Task created successfully');
});

Implementing PUT and PATCH Routes

Updates can be handled in two ways. PUT implies replacing the entire resource with a new representation, while PATCH implies a partial update.

To identify which item to update, we use dynamic route parameters, which you learned about in our guide on routing in Express.

JAVASCRIPT
// PUT: Replace the whole task
app.put(CE9178">'/tasks/:id', (req, res) => {
  const { id } = req.params;
  res.send(CE9178">`Task ${id} has been fully replaced`);
});

// PATCH: Update only the CE9178">'completed' status
app.patch(CE9178">'/tasks/:id', (req, res) => {
  const { id } = req.params;
  res.send(CE9178">`Task ${id} has been partially updated`);
});

Implementing a DELETE Route

The DELETE method is straightforward: it tells the server to remove the resource identified by the URL. If the operation is successful, we typically return a 204 No Content status or a confirmation message.

JAVASCRIPT
app.delete(CE9178">'/tasks/:id', (req, res) => {
  const { id } = req.params;
  res.status(200).send(CE9178">`Task ${id} deleted`);
});

Hands-on Exercise: Building the Task API

Creative young man working on a strategy plan on a whiteboard at the office.

Let's advance our running project. Open your app.js file and add the following routes to your existing Express instance:

  1. Create a POST /tasks route that returns a 201 status code.
  2. Create a PUT /tasks/:id route that logs "Updating task" to the console.
  3. Create a DELETE /tasks/:id route that returns a simple success message.

Test these using a tool like Postman or curl from your terminal: curl -X POST http://localhost:3000/tasks

Common Pitfalls

  • Forgetting Status Codes: Using 200 OK for a successful creation is technically valid but semantically incorrect. Use 201 Created for POST requests to signal that a new resource now exists.
  • Confusing PUT and PATCH: Don't use PUT if you only want to change one field; it forces you to send the entire object structure from the client, which is inefficient and error-prone.
  • Missing ID Validation: Always ensure that if a user tries to DELETE or PUT a resource, you check if that resource actually exists before attempting the operation.

FAQ

Yellow letter tiles spell 'questions' on a contrasting blue background.

Q: Can I use GET to delete a resource? A: Technically yes, but you shouldn't. GET is a "safe" method, meaning it should never change the state of your server. Always use the appropriate method to keep your API predictable.

Q: Do I need to install extra libraries to handle these methods? A: No, Express handles all standard HTTP methods natively out of the box.

Recap

Team members presenting a project in a modern office setting with a focus on collaboration.

We have now implemented the core verbs of any REST API. By mapping POST to creation, PUT/PATCH to modification, and DELETE to removal, you’ve turned a static server into an interactive service. These methods form the backbone of how your frontend or mobile app will communicate with your backend.

Up next: Understanding Middleware — where we learn how to intercept these requests to add functionality like logging and authentication.

Similar Posts