Bird
Raised Fist0
Expressframework~10 mins

HTTP methods for CRUD operations in Express - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - HTTP methods for CRUD operations
Client sends HTTP request
Server receives request
Check HTTP method
Read
Send response with data or status
End
This flow shows how a server handles different HTTP methods to perform CRUD operations: GET to read, POST to create, PUT to update, and DELETE to remove data.
Execution Sample
Express
app.get('/items', (req, res) => res.send('Read items'));
app.post('/items', (req, res) => res.send('Create item'));
app.put('/items/:id', (req, res) => res.send('Update item'));
app.delete('/items/:id', (req, res) => res.send('Delete item'));
This code sets up routes in Express to handle CRUD operations using HTTP methods.
Execution Table
StepHTTP MethodRequest URLServer ActionResponse Sent
1GET/itemsRead all items from databaseSend list of items
2POST/itemsAdd new item to databaseSend confirmation of creation
3PUT/items/5Update item with id=5Send confirmation of update
4DELETE/items/5Remove item with id=5Send confirmation of deletion
5PATCH/items/5Not handled in this exampleSend 404 or method not allowed
6GET/unknownNo matching routeSend 404 Not Found
💡 Execution stops after sending response for each request.
Variable Tracker
VariableStartAfter GETAfter POSTAfter PUTAfter DELETEFinal
items[][item1, item2][item1, item2, newItem][item1, updatedItem, newItem][item1, newItem][item1, newItem]
Key Moments - 3 Insights
Why does the server use different HTTP methods for the same URL?
Because each HTTP method tells the server what action to perform on the resource. For example, GET reads data, POST creates new data, PUT updates existing data, and DELETE removes data. See execution_table rows 1-4.
What happens if the client sends a method not handled by the server?
The server responds with an error like 404 Not Found or Method Not Allowed because no route matches that method. See execution_table row 5.
How does the server know which item to update or delete?
The server uses the URL parameter (like /items/5) to identify the specific item by its id. This is shown in execution_table rows 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does the server send for a POST request to /items?
ASend list of items
BSend confirmation of deletion
CSend confirmation of creation
DSend 404 Not Found
💡 Hint
Check execution_table row 2 under 'Response Sent'
At which step does the server update an existing item?
AStep 3
BStep 1
CStep 4
DStep 2
💡 Hint
Look at execution_table row 3 under 'Server Action'
If the client sends a DELETE request to /items/10, what will the server do?
ACreate a new item
BRemove item with id=10
CUpdate item with id=10
DSend list of items
💡 Hint
See execution_table row 4 for DELETE method behavior
Concept Snapshot
HTTP methods map to CRUD:
GET = Read data
POST = Create data
PUT = Update data
DELETE = Remove data
Express routes handle these methods to perform actions on resources.
Full Transcript
This lesson shows how Express uses HTTP methods to perform CRUD operations. The server listens for requests with methods GET, POST, PUT, and DELETE. Each method triggers a different action: reading data, creating new data, updating existing data, or deleting data. The server uses the URL and method to decide what to do and sends back a response. If a method is not handled, the server returns an error. This helps clients interact with data in a clear, organized way.

Practice

(1/5)
1. Which HTTP method is typically used in Express to retrieve data from a server?
easy
A. PUT
B. POST
C. DELETE
D. GET

Solution

  1. Step 1: Understand HTTP methods purpose

    GET is used to read or retrieve data from the server without changing it.
  2. Step 2: Match method to action

    Since the question asks for retrieving data, GET is the correct method.
  3. Final Answer:

    GET -> Option D
  4. Quick Check:

    Retrieve data = GET [OK]
Hint: GET always reads data without changing it [OK]
Common Mistakes:
  • Confusing POST with GET for reading data
  • Using DELETE to get data
  • Thinking PUT retrieves data
2. Which of the following is the correct Express syntax to handle a POST request to the path /users?
easy
A. app.get('/users', (req, res) => { ... })
B. app.put('/users', (req, res) => { ... })
C. app.post('/users', (req, res) => { ... })
D. app.delete('/users', (req, res) => { ... })

Solution

  1. Step 1: Identify method for creating data

    POST is used to create new data on the server.
  2. Step 2: Match Express syntax to POST

    app.post('/users', ...) correctly handles POST requests to /users.
  3. Final Answer:

    app.post('/users', (req, res) => { ... }) -> Option C
  4. Quick Check:

    POST creates data = app.post() [OK]
Hint: Use app.post() for creating new data routes [OK]
Common Mistakes:
  • Using app.get() for POST requests
  • Confusing app.put() with app.post()
  • Writing app.delete() for creation
3. What will be the response status code if you define this Express route and send a request to PUT /items/5?
app.put('/items/:id', (req, res) => {
  res.status(200).send(`Updated item ${req.params.id}`);
});
medium
A. 200 OK
B. 201 Created
C. 500 Internal Server Error
D. 404 Not Found

Solution

  1. Step 1: Analyze route and method

    The route handles PUT requests to /items/:id, so /items/5 matches with id=5.
  2. Step 2: Check response status code

    The code explicitly sets status 200 and sends a message, so response status is 200 OK.
  3. Final Answer:

    200 OK -> Option A
  4. Quick Check:

    PUT route sends status 200 = 200 OK [OK]
Hint: Check res.status() for response code [OK]
Common Mistakes:
  • Assuming 201 Created for PUT
  • Thinking 404 if route exists
  • Confusing 500 error without code
4. Identify the error in this Express route meant to delete a user by ID:
app.delete('/users/:id', (req, res) => {
  const userId = req.params;
  deleteUser(userId);
  res.send('User deleted');
});
medium
A. req.params should be req.params.id to get the ID
B. deleteUser is not a valid Express method
C. res.send should be res.status(204).send()
D. Route path should be '/user/:id' not '/users/:id'

Solution

  1. Step 1: Check how userId is assigned

    req.params is an object; to get the ID string, use req.params.id.
  2. Step 2: Understand deleteUser usage

    deleteUser likely expects an ID string, so passing the whole params object is wrong.
  3. Final Answer:

    req.params should be req.params.id to get the ID -> Option A
  4. Quick Check:

    Use req.params.id for ID value [OK]
Hint: Use req.params.id to access route parameters [OK]
Common Mistakes:
  • Passing whole req.params object instead of ID
  • Confusing route path naming
  • Assuming deleteUser is Express built-in
5. You want to update a user's email fully using Express. Which HTTP method and route setup is best practice for this operation?
hard
A. app.post('/users/:id', (req, res) => { ... })
B. app.put('/users/:id', (req, res) => { ... })
C. app.get('/users/:id', (req, res) => { ... })
D. app.delete('/users/:id', (req, res) => { ... })

Solution

  1. Step 1: Identify method for full update

    PUT is the HTTP method used to fully update existing data.
  2. Step 2: Match route to user ID

    Route '/users/:id' targets a specific user by ID for update.
  3. Final Answer:

    app.put('/users/:id', (req, res) => { ... }) -> Option B
  4. Quick Check:

    Full update = PUT method [OK]
Hint: Use PUT with ID route for full updates [OK]
Common Mistakes:
  • Using POST for updates instead of creation
  • Using GET or DELETE for updating data
  • Not including :id in route for specific user