0
0
Node.jsframework~20 mins

Routing requests manually in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Manual Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
AThe server responds with 'Not Found' and status 404.
BThe server responds with 'About Page' and status 200.
CThe server responds with 'Home Page' and status 200.
DThe server throws an error and crashes.
Attempts:
2 left
💡 Hint
Check the condition that matches the request method and URL.
📝 Syntax
intermediate
2: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?
Aif (req.method === 'GET' && req.url === '/') { res.end('OK'); }
Bif (req.method === 'GET' && req.url === '/about') { res.end('About'); }
Cif (req.method === 'POST' && req.url === '/submit') { res.end('Submitted'); }
Dif req.method === 'GET' && req.url === '/' { res.end('OK'); }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
🔧 Debug
advanced
2: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);
AThe single '=' assigns 'GET' to req.method, but the condition is always false, so 'Not Found' is sent.
BThe single '=' assigns 'GET' to req.method, so the condition is always true and 'Hello' is sent.
CThe code throws a runtime error because req.method is read-only.
DThe server never starts because of a syntax error.
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
state_output
advanced
2: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);
AThe server responds with 'Submitted' and status 201.
BThe server responds with 'Home' and status 200.
CThe server responds with 'Not Found' and status 404.
DThe server throws an error because POST is not handled.
Attempts:
2 left
💡 Hint
Check the condition that matches POST requests to '/submit'.
🧠 Conceptual
expert
2: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?
AManual routing requires writing repetitive code for each route, making it hard to scale and maintain.
BManual routing automatically handles query parameters and middleware, which can cause unexpected behavior.
CManual routing prevents the server from handling concurrent requests efficiently.
DManual routing forces the use of synchronous code, blocking the event loop.
Attempts:
2 left
💡 Hint
Think about code complexity and maintenance.