Bird
0
0

Which of the following is the correct way to create a basic HTTP server in Node.js using ES modules?

easy📝 Syntax Q12 of 15
Node.js - HTTP Module
Which of the following is the correct way to create a basic HTTP server in Node.js using ES modules?
Aimport http from 'http'; http.createServer((req, res) => res.end('Hello')).listen(3000);
Bconst http = require('http'); http.createServer((req, res) => res.end('Hello')).listen(3000);
Cconst http = import('http'); http.createServer((req, res) => res.end('Hello')).listen(3000);
Dconst http = require('http'); http.createServer((req, res) => res.send('Hello')).listen(3000);
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes