📖 Scenario: You are building a simple Node.js server to handle user data. You want to connect HTTP methods to CRUD operations so your server can create, read, update, and delete users.
🎯 Goal: Build a basic Node.js server using the http module that maps HTTP methods GET, POST, PUT, and DELETE to CRUD operations on a simple in-memory user list.
📋 What You'll Learn
Create an array called users with three user names: 'Alice', 'Bob', and 'Charlie'
Create a variable called port and set it to 3000
Use http.createServer to create a server that listens for HTTP requests
Inside the server callback, use req.method to check the HTTP method and handle GET, POST, PUT, and DELETE accordingly
💡 Why This Matters
🌍 Real World
Web servers use HTTP methods to perform CRUD operations on data. Understanding this mapping helps build APIs that clients like browsers or apps can use to manage data.
💼 Career
Backend developers often create RESTful APIs that map HTTP methods to CRUD actions. This knowledge is essential for building web services and working with frameworks like Express.js or Fastify.
Progress0 / 4 steps
1
Create the initial user data array
Create an array called users with these exact strings: 'Alice', 'Bob', and 'Charlie'.
Node.js
Hint
Use const users = ['Alice', 'Bob', 'Charlie']; to create the array.
2
Set the server port
Create a variable called port and set it to the number 3000.
Node.js
Hint
Use const port = 3000; to set the port number.
3
Create the HTTP server with method checks
Use http.createServer to create a server. Inside the callback, use req.method to check for 'GET', 'POST', 'PUT', and 'DELETE' methods. For now, just write empty blocks for each method inside the callback function.
Node.js
Hint
Use http.createServer((req, res) => { ... }) and inside check req.method with if and else if.
4
Complete the server to respond with user data and start listening
Inside the 'GET' block, respond with the JSON string of users. Inside the 'POST', 'PUT', and 'DELETE' blocks, respond with a simple text message like 'Create user', 'Update user', and 'Delete user' respectively. Finally, call server.listen(port) to start the server.
Node.js
Hint
Use res.setHeader and res.end to send responses. Use server.listen(port) to start the server.
Practice
(1/5)
1. Which HTTP method is typically used to retrieve data from a server in a RESTful API?
easy
A. GET
B. POST
C. PUT
D. DELETE
Solution
Step 1: Understand HTTP methods and their purposes
In RESTful APIs, GET is used to read or retrieve data from the server.
Step 2: Match the method to the action
POST creates, PUT updates, and DELETE removes data, so only GET fits retrieving data.
Final Answer:
GET -> Option A
Quick Check:
Read = GET [OK]
Hint: GET is for reading data, like looking at a book [OK]
Common Mistakes:
Confusing POST with GET
Using DELETE to fetch data
Thinking PUT retrieves data
2. Which of the following Express route definitions correctly handles updating a resource using the HTTP method for update?
easy
A. app.get('/item/:id', handler)
B. app.post('/item/:id', handler)
C. app.put('/item/:id', handler)
D. app.delete('/item/:id', handler)
Solution
Step 1: Identify the HTTP method for updating
Updating a resource uses the PUT method in RESTful APIs.
Step 2: Match Express method to HTTP method
Express uses app.put() to handle PUT requests, so app.put('/item/:id', handler) is correct.
Final Answer:
app.put('/item/:id', handler) -> Option C
Quick Check:
Update = PUT = app.put() [OK]
Hint: Use app.put() for updates in Express routes [OK]
Common Mistakes:
Using app.post() for updates
Using app.get() for updates
Confusing app.delete() with update
3. Consider this Express code snippet:
app.delete('/user/:id', (req, res) => {
res.send(`Deleted user ${req.params.id}`);
});
// Client sends DELETE request to /user/42
What will the server respond with?
medium
A. "Updated user 42"
B. "Deleted user 42"
C. "Created user 42"
D. 404 Not Found
Solution
Step 1: Understand the route and HTTP method
The route listens for DELETE requests on /user/:id. The client sends a DELETE request to /user/42.
Step 2: Analyze the response sent
The handler sends back the string Deleted user 42 using the id from the URL.
Final Answer:
"Deleted user 42" -> Option B
Quick Check:
DELETE request responds with deletion message [OK]
Hint: DELETE routes respond with deletion confirmation [OK]
Common Mistakes:
Confusing DELETE with POST response
Expecting 404 instead of success message
Mixing up URL parameters
4. What is wrong with this Express route for creating a new item?
app.get('/items', (req, res) => {
// code to add new item
res.send('Item created');
});
medium
A. Missing route parameter for item ID
B. Route path should be '/item' not '/items'
C. Response message is incorrect
D. GET method should not be used to create data
Solution
Step 1: Identify the HTTP method used
The route uses GET, which is meant for reading data, not creating.
Step 2: Match method to CRUD action
Creating new data should use POST, so using GET here is incorrect.
Final Answer:
GET method should not be used to create data -> Option D
Quick Check:
Create = POST, not GET [OK]
Hint: Use POST to create, not GET [OK]
Common Mistakes:
Using GET to create resources
Ignoring HTTP method semantics
Assuming route path is the issue
5. You want to build an Express API that allows clients to create, read, update, and delete "products". Which set of route definitions correctly maps HTTP methods to CRUD actions?
hard
A. app.post('/products', createHandler); app.get('/products/:id', readHandler); app.put('/products/:id', updateHandler); app.delete('/products/:id', deleteHandler);
B. app.get('/products', createHandler); app.post('/products/:id', readHandler); app.put('/products', updateHandler); app.delete('/products', deleteHandler);
C. app.put('/products', createHandler); app.get('/products/:id', readHandler); app.post('/products/:id', updateHandler); app.delete('/products/:id', deleteHandler);
D. app.delete('/products', createHandler); app.put('/products/:id', readHandler); app.post('/products/:id', updateHandler); app.get('/products/:id', deleteHandler);
Solution
Step 1: Match each CRUD action to correct HTTP method
app.post('/products', createHandler); app.get('/products/:id', readHandler); app.put('/products/:id', updateHandler); app.delete('/products/:id', deleteHandler); uses POST /products for create, GET /products/:id for read, PUT /products/:id for update, and DELETE /products/:id for delete, which is correct.