Bird
Raised Fist0
Node.jsframework~20 mins

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

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
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.

Practice

(1/5)
1. Why is building an HTTP server important in Node.js?
easy
A. It automatically fixes bugs in your code.
B. It makes your computer run faster.
C. It allows your computer to share information over the internet.
D. It helps you write desktop applications.

Solution

  1. Step 1: Understand the role of HTTP servers

    HTTP servers let computers send and receive information over the internet.
  2. Step 2: Connect this to Node.js usage

    Node.js provides tools to build these servers so your app can communicate online.
  3. Final Answer:

    It allows your computer to share information over the internet. -> Option C
  4. Quick Check:

    HTTP servers = share info online [OK]
Hint: HTTP servers share info online, not speed or desktop apps [OK]
Common Mistakes:
  • Thinking HTTP servers speed up the computer
  • Confusing servers with desktop app tools
  • Believing servers fix code bugs automatically
2. Which of the following is the correct way to create a basic HTTP server in Node.js using ES modules?
easy
A. import http from 'http'; http.createServer((req, res) => res.end('Hello')).listen(3000);
B. const http = require('http'); http.createServer((req, res) => res.end('Hello')).listen(3000);
C. const http = import('http'); http.createServer((req, res) => res.end('Hello')).listen(3000);
D. const http = require('http'); http.createServer((req, res) => res.send('Hello')).listen(3000);

Solution

  1. Step 1: Identify modern Node.js import syntax

    Node.js ES modules use import to load modules, not require.
  2. Step 2: Check server creation and response method

    http.createServer with res.end() is correct; res.send() is not a Node.js method.
  3. Final Answer:

    import http from 'http'; http.createServer((req, res) => res.end('Hello')).listen(3000); -> Option A
  4. Quick Check:

    Use import + res.end() in Node.js 20+ [OK]
Hint: Use import and res.end() for Node.js HTTP servers [OK]
Common Mistakes:
  • Using require instead of import in ES modules
  • Using res.send() which is not in Node.js core
  • Trying to import with const and import() function
3. What will this Node.js HTTP server print when accessed?
import http from 'http';
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Welcome!');
});
server.listen(3000);
medium
A. Error: statusCode is not a property
B. Welcome!
C. undefined
D. Content-Type header missing

Solution

  1. Step 1: Analyze server response setup

    The server sets status code 200, sets Content-Type header, and ends response with 'Welcome!'.
  2. Step 2: Understand what client receives

    The client will get a plain text response with 'Welcome!' and status 200.
  3. Final Answer:

    Welcome! -> Option B
  4. Quick Check:

    res.end('Welcome!') sends text response [OK]
Hint: res.end sends the response body text [OK]
Common Mistakes:
  • Thinking statusCode is invalid property
  • Expecting undefined instead of response text
  • Ignoring headers set by setHeader
4. Identify the error in this Node.js HTTP server code:
import http from 'http';
const server = http.createServer((req, res) => {
  res.write('Hello');
  res.write('World');
  res.end();
});
server.listen(3000);
medium
A. No error, code works fine
B. Cannot call res.write multiple times
C. Missing res.end() call
D. res.end() should have a string argument

Solution

  1. Step 1: Review usage of res.write and res.end

    Node.js allows multiple res.write calls before res.end to send chunks.
  2. Step 2: Confirm res.end usage

    Calling res.end() without argument is valid; it ends the response.
  3. Final Answer:

    No error, code works fine -> Option A
  4. Quick Check:

    Multiple res.write calls + res.end() is valid [OK]
Hint: Multiple res.write calls are allowed before res.end [OK]
Common Mistakes:
  • Thinking res.end must have argument
  • Believing multiple res.write calls cause error
  • Forgetting res.end is needed to finish response
5. You want to build a Node.js HTTP server that responds with JSON data and handles errors gracefully. Which approach is best?
hard
A. Use http.createServer, set Content-Type to application/json, but do not handle errors.
B. Use http.createServer, send JSON as plain text, ignore errors to keep server fast.
C. Use http.createServer, set Content-Type to text/html, and send JSON string directly.
D. Use http.createServer, set Content-Type to application/json, and wrap response code in try-catch.

Solution

  1. Step 1: Set correct Content-Type for JSON

    To send JSON, Content-Type must be 'application/json' so clients parse it properly.
  2. Step 2: Handle errors gracefully

    Wrapping response code in try-catch prevents server crashes and sends error info.
  3. Final Answer:

    Use http.createServer, set Content-Type to application/json, and wrap response code in try-catch. -> Option D
  4. Quick Check:

    JSON needs correct header + error handling [OK]
Hint: Always set JSON header and catch errors in server code [OK]
Common Mistakes:
  • Sending JSON with wrong Content-Type
  • Ignoring errors causing server crashes
  • Using text/html header for JSON data