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
Recall & Review
beginner
What is an HTTP server in Node.js?
An HTTP server in Node.js listens for requests from web browsers or clients and sends back responses. It acts like a waiter taking orders and delivering food in a restaurant.
Click to reveal answer
beginner
Why do we build HTTP servers?
We build HTTP servers to let users access websites, apps, or services over the internet. They handle requests and send back the right data or pages.
Click to reveal answer
beginner
How does an HTTP server help in real life?
It connects people to online shops, social media, games, and more. Without servers, websites wouldn’t work and we couldn’t use many internet services.
Click to reveal answer
intermediate
What role does Node.js play in building HTTP servers?
Node.js lets you write server code using JavaScript. It’s fast and good at handling many users at once, making it popular for building HTTP servers.
Click to reveal answer
beginner
What happens when an HTTP server receives a request?
The server reads the request, decides what the user wants, and sends back the right response like a webpage, data, or an error message.
Click to reveal answer
What does an HTTP server do?
AListens for requests and sends responses
BCreates web browsers
CStores files on your computer
DRuns desktop applications
✗ Incorrect
An HTTP server listens for requests from clients and sends back responses like web pages or data.
Why is Node.js popular for building HTTP servers?
AIt only works on Windows
BIt uses JavaScript and handles many users efficiently
CIt is a type of database
DIt creates mobile apps
✗ Incorrect
Node.js uses JavaScript and is good at handling many connections at once, making it great for servers.
What is a real-life example of an HTTP server's role?
ACharging your phone battery
BPrinting documents
CRunning video games on your console
DDelivering web pages when you visit a website
✗ Incorrect
HTTP servers deliver web pages and data when you visit websites.
What happens if an HTTP server cannot find the requested page?
AIt sends an error message like 404 Not Found
BIt shuts down
CIt deletes the website
DIt sends a blank page
✗ Incorrect
The server sends an error response like 404 to tell the user the page is missing.
Which language do you use to write HTTP servers in Node.js?
AC++
BPython
CJavaScript
DJava
✗ Incorrect
Node.js uses JavaScript to write server code.
Explain in simple terms why building HTTP servers matters for the internet.
Think about how you get web pages on your browser.
You got /4 concepts.
Describe how Node.js helps in creating HTTP servers and why it is useful.
Consider what makes Node.js special for server tasks.
You got /4 concepts.
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
Step 1: Understand the role of HTTP servers
HTTP servers let computers send and receive information over the internet.
Step 2: Connect this to Node.js usage
Node.js provides tools to build these servers so your app can communicate online.
Final Answer:
It allows your computer to share information over the internet. -> Option C
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
Step 1: Identify modern Node.js import syntax
Node.js ES modules use import to load modules, not require.
Step 2: Check server creation and response method
http.createServer with res.end() is correct; res.send() is not a Node.js method.
Final Answer:
import http from 'http'; http.createServer((req, res) => res.end('Hello')).listen(3000); -> Option A
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
Step 1: Analyze server response setup
The server sets status code 200, sets Content-Type header, and ends response with 'Welcome!'.
Step 2: Understand what client receives
The client will get a plain text response with 'Welcome!' and status 200.
Final Answer:
Welcome! -> Option B
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
Step 1: Review usage of res.write and res.end
Node.js allows multiple res.write calls before res.end to send chunks.
Step 2: Confirm res.end usage
Calling res.end() without argument is valid; it ends the response.
Final Answer:
No error, code works fine -> Option A
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
Step 1: Set correct Content-Type for JSON
To send JSON, Content-Type must be 'application/json' so clients parse it properly.
Step 2: Handle errors gracefully
Wrapping response code in try-catch prevents server crashes and sends error info.
Final Answer:
Use http.createServer, set Content-Type to application/json, and wrap response code in try-catch. -> Option D
Quick Check:
JSON needs correct header + error handling [OK]
Hint: Always set JSON header and catch errors in server code [OK]