Bird
0
0

What will the following Node.js code output when accessed via a browser?

medium📝 component behavior Q13 of 15
Node.js - HTTP Module
What will the following Node.js code output when accessed via a browser?
const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World');
});
server.listen(4000);
AHello World
BError: server.listen is not a function
C404 Not Found
DHello
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the server response setup

    The server sets the HTTP status to 200 (OK) and content type to plain text, then sends 'Hello World' as the response body.
  2. Step 2: Understand what the browser receives

    When accessed, the browser will display the exact string 'Hello World' from the server response.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    Response text = Hello World [OK]
Quick Trick: 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()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes