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.

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:
| Method | CRUD Operation | Purpose |
|---|---|---|
GET | Read | Fetch a resource |
POST | Create | Submit new data to be processed |
PUT | Update | Replace an entire resource |
PATCH | Update | Modify a specific part of a resource |
DELETE | Delete | Remove 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().
JAVASCRIPTapp.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.
JAVASCRIPTapp.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

Let's advance our running project. Open your app.js file and add the following routes to your existing Express instance:
- Create a
POST /tasksroute that returns a 201 status code. - Create a
PUT /tasks/:idroute that logs "Updating task" to the console. - Create a
DELETE /tasks/:idroute 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 OKfor a successful creation is technically valid but semantically incorrect. Use201 CreatedforPOSTrequests to signal that a new resource now exists. - Confusing PUT and PATCH: Don't use
PUTif 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
DELETEorPUTa resource, you check if that resource actually exists before attempting the operation.
FAQ

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

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.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.


