Challenge - 5 Problems
Manual Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a GET request is made to '/'?
Consider this Node.js server code that manually routes requests. What will the server respond with when a GET request is made to the root path '/'?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.method === 'GET' && req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Home Page'); } else if (req.method === 'GET' && req.url === '/about') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('About Page'); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); } }); server.listen(3000);
Attempts:
2 left
💡 Hint
Check the condition that matches the request method and URL.
✗ Incorrect
The code checks if the request method is GET and the URL is '/'. If so, it responds with 'Home Page' and status 200. Other paths or methods get different responses.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in manual routing?
Look at these code snippets for routing requests manually in Node.js. Which one will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
✗ Incorrect
Option D is missing parentheses around the condition in the if statement, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this manual routing code always respond with 'Not Found'?
This Node.js server code is supposed to respond with 'Hello' on GET '/' requests, but it always responds with 'Not Found'. What is the cause?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.method = 'GET' && req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello'); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); } }); server.listen(3000);
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
✗ Incorrect
Using '=' assigns 'GET' to req.method, which is truthy, so the condition is always true and 'Hello' is sent. This is a bug because it should use '===' to compare.
❓ state_output
advanced2:00remaining
What is the response when a POST request is made to '/submit'?
Given this manual routing code, what will the server respond with when a POST request is made to '/submit'?
Node.js
import http from 'http'; const server = http.createServer((req, res) => { if (req.method === 'GET' && req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Home'); } else if (req.method === 'POST' && req.url === '/submit') { res.writeHead(201, { 'Content-Type': 'text/plain' }); res.end('Submitted'); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not Found'); } }); server.listen(3000);
Attempts:
2 left
💡 Hint
Check the condition that matches POST requests to '/submit'.
✗ Incorrect
The code explicitly checks for POST requests to '/submit' and responds with status 201 and 'Submitted'.
🧠 Conceptual
expert2:00remaining
What is a key limitation of manual routing in Node.js HTTP servers?
When manually routing requests in Node.js using the http module, what is a main limitation compared to using a routing framework?
Attempts:
2 left
💡 Hint
Think about code complexity and maintenance.
✗ Incorrect
Manual routing requires explicit if-else checks for each route, leading to repetitive and hard-to-maintain code as the app grows. Frameworks provide cleaner abstractions.