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
Creating a basic HTTP server
📖 Scenario: You want to create a simple web server that can respond to requests on your computer. This server will listen for visitors and send back a friendly message.
🎯 Goal: Build a basic HTTP server using Node.js that listens on port 3000 and responds with a plain text message.
📋 What You'll Learn
Create an HTTP server using Node.js built-in modules
Set the server to listen on port 3000
Respond with a plain text message 'Hello, world!' to every request
Use proper headers to indicate the content type
💡 Why This Matters
🌍 Real World
Basic HTTP servers are the foundation of web applications and APIs. Knowing how to create one helps you understand how web communication works.
💼 Career
Many backend developer roles require knowledge of creating and managing servers. This skill is essential for building web services and applications.
Progress0 / 4 steps
1
Import the HTTP module
Write a line of code to import the built-in Node.js http module and assign it to a variable called http.
Node.js
Hint
Use require('http') to import the HTTP module.
2
Create the server variable
Create a variable called server and assign it the result of http.createServer() with a function that takes req and res as parameters.
Node.js
Hint
Use http.createServer((req, res) => { }) to create the server.
3
Add response headers and message
Inside the server function, use res.writeHead(200, {'Content-Type': 'text/plain'}) to set the status and headers, then use res.end('Hello, world!') to send the response.
Node.js
Hint
Use res.writeHead to set headers and res.end to send the message.
4
Make the server listen on port 3000
Call server.listen(3000) to make the server listen on port 3000.
Node.js
Hint
Use server.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What does the http.createServer() function do in Node.js?
easy
A. It compiles JavaScript code into machine code.
B. It creates a database connection for storing data.
C. It creates a server that listens for HTTP requests and sends responses.
D. It formats JSON data for sending over the network.
Solution
Step 1: Understand the purpose of http.createServer()
This function sets up a server that waits for HTTP requests from clients like browsers.
Step 2: Identify what the server does
The server uses a function to handle incoming requests and send back responses, enabling communication over the web.
Final Answer:
It creates a server that listens for HTTP requests and sends responses. -> Option C
Quick Check:
HTTP server creation = It creates a server that listens for HTTP requests and sends responses. [OK]
Hint: Remember: createServer sets up the web server [OK]
Common Mistakes:
Confusing server creation with database connection
Thinking it compiles code
Mixing up data formatting with server setup
2. Which of the following is the correct way to start a Node.js HTTP server on port 3000?
easy
A. http.listen(3000);
B. server.listen(3000);
C. server.start(3000);
D. server.run(3000);
Solution
Step 1: Identify the method to start the server
The correct method to make the server listen on a port is listen().
Step 2: Match the method with the server object
Calling server.listen(3000); starts the server on port 3000.
Final Answer:
server.listen(3000); -> Option B
Quick Check:
Start server with listen() = server.listen(3000); [OK]
Hint: Use listen() to start the server on a port [OK]
Common Mistakes:
Using non-existent methods like start() or run()
Calling listen() on http instead of server
Confusing server methods with other modules
3. What will the following Node.js code output when accessed via a browser?
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]