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 Node.js module is used to create a basic HTTP server?
The http module is used to create a basic HTTP server in Node.js.
Click to reveal answer
beginner
What method starts the server and listens for requests?
The server.listen(port, callback) method starts the server and listens on the specified port.
Click to reveal answer
beginner
In the callback for http.createServer(), what are the two main objects provided?
The callback receives request and response objects. request contains info about the incoming request, and response is used to send data back to the client.
Click to reveal answer
beginner
How do you send a plain text response 'Hello World' to the client?
Use response.writeHead(200, {'Content-Type': 'text/plain'}) to set status and headers, then response.end('Hello World') to send the response and close the connection.
Click to reveal answer
beginner
Why is it important to call response.end() in a Node.js HTTP server?
Calling response.end() signals that the response is complete. Without it, the client will keep waiting and the connection stays open.
Click to reveal answer
Which Node.js module do you import to create an HTTP server?
Ahttp
Bfs
Cpath
Durl
✗ Incorrect
The http module provides the tools to create an HTTP server.
What method do you use to start listening for incoming requests?
Aserver.start()
Bhttp.createServer()
Cserver.listen()
Dhttp.listen()
✗ Incorrect
The listen() method starts the server and listens on a specified port.
In the server callback, which object contains information about the client's request?
Aserver
Brequest
Cresponse
Dclient
✗ Incorrect
The request object holds details about the incoming client request.
How do you send a response header with status 200 and content type 'text/plain'?
The server sets the HTTP status to 200 (OK) and content type to plain text, then sends 'Hello World' as the response body.
Step 2: Understand what the browser receives
When accessed, the browser will display the exact string 'Hello World' from the server response.
Final Answer:
Hello World -> Option A
Quick Check:
Response text = Hello World [OK]
Hint: Check res.end() content for output text [OK]
Common Mistakes:
Expecting an error due to incorrect method
Confusing status codes with output text
Assuming partial output without reading res.end()
4. Identify the error in this Node.js HTTP server code:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Welcome');
res.end();
});
server.listen(8080)
console.log('Server running on port 8080');
medium
A. Incorrect Content-Type header value
B. Using res.write() without res.end() causes error
C. Missing semicolon after server.listen(8080)
D. No error; code runs correctly and serves 'Welcome'
Solution
Step 1: Check syntax and method usage
The code correctly calls res.write() followed by res.end(), which is valid to send response data.
Step 2: Verify headers and server start
The Content-Type is valid as 'text/html', and server.listen(8080) starts the server properly. Missing semicolons are optional in JavaScript.
Final Answer:
No error; code runs correctly and serves 'Welcome' -> Option D
Quick Check:
Valid server code = No error; code runs correctly and serves 'Welcome' [OK]
Hint: res.write() + res.end() is valid to send response [OK]
Common Mistakes:
Thinking missing semicolons cause errors
Believing res.write() must be avoided
Assuming wrong Content-Type causes failure
5. You want to create a Node.js HTTP server that responds with JSON data {"status":"ok"} and sets the correct header. Which code snippet correctly does this?
hard
A. const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
});
B. const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"status":"ok"}');
});
C. const server = http.createServer((req, res) => {
res.writeHead(404, {'Content-Type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
});
D. const server = http.createServer((req, res) => {
res.writeHead(200);
res.end({status: 'ok'});
});
Solution
Step 1: Set correct Content-Type header for JSON
The header must be 'application/json' to tell the client the response is JSON data.
Step 2: Send JSON string as response body
Use JSON.stringify() to convert the object to a JSON string before sending with res.end().
Final Answer:
Code snippet with application/json header and JSON.stringify() -> Option A
Quick Check:
JSON header + stringified data = const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
}); [OK]
Hint: Use application/json header and JSON.stringify() [OK]