0
0
Node.jsframework~20 mins

Why building HTTP servers matters in Node.js - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use HTTP servers in Node.js?
Which of the following best explains why building HTTP servers is important in Node.js?
AHTTP servers are only used to send emails from Node.js.
BHTTP servers allow Node.js to listen for and respond to web requests, enabling web applications to work.
CHTTP servers are used to store files on the computer permanently.
DHTTP servers help Node.js to run desktop applications faster.
Attempts:
2 left
💡 Hint
Think about what happens when you open a website in your browser.
component_behavior
intermediate
2:00remaining
What happens when a Node.js HTTP server receives a request?
Consider a simple Node.js HTTP server. What does the server do when it receives a request from a browser?
AIt processes the request and sends back a response like a web page or data.
BIt immediately shuts down to save resources.
CIt ignores the request and waits for a different type of input.
DIt sends the request to the operating system to handle.
Attempts:
2 left
💡 Hint
Think about what a web server's job is when you visit a website.
📝 Syntax
advanced
2:30remaining
Identify the correct way to create a basic HTTP server in Node.js
Which code snippet correctly creates a simple HTTP server that responds with 'Hello World'?
Node.js
const http = require('http');

// Choose the correct server creation code below
Aconst server = http.createServer((req, res) => { res.write('Hello World'); res.end(); });
Bconst server = http.createServer((req, res) => { res.send('Hello World'); });
Cconst server = http.createServer((request, response) => { response.writeHead(200); response.end('Hello World'); });
Dconst server = http.createServer((req, res) => { res.write('Hello World'); })
Attempts:
2 left
💡 Hint
Remember to send a status code and end the response properly.
🔧 Debug
advanced
2:30remaining
Why does this Node.js HTTP server code cause the browser to hang?
Look at this code snippet: const http = require('http'); const server = http.createServer((req, res) => { res.write('Loading...'); }); server.listen(3000); Why does the browser keep loading and never show the response?
ABecause the callback function is missing the request parameter.
BBecause the server.listen port is incorrect.
CBecause res.write() cannot be used in HTTP servers.
DBecause res.end() is missing, so the response never finishes.
Attempts:
2 left
💡 Hint
Think about how HTTP responses are completed.
state_output
expert
3:00remaining
What is the output when multiple requests hit this Node.js HTTP server?
Consider this server code: const http = require('http'); let count = 0; const server = http.createServer((req, res) => { count++; res.writeHead(200, {'Content-Type': 'text/plain'}); res.end(`Request number: ${count}`); }); server.listen(3000); If three requests come in one after another, what will the third request receive as a response?
A"Request number: 3"
B"Request number: 1"
C"Request number: 0"
DAn error because count is not reset
Attempts:
2 left
💡 Hint
Think about how the variable count changes with each request.