JavaScript Node.js Basics 4 — Questions and Answers
Question 1: What is the correct way to create a simple HTTP server in Node.js that responds with 'Hello World'?
- http.createServer((req, res) => { res.send('Hello World'); }).listen(3000);
- http.createServer((req, res) => { res.writeHead(200); res.end('Hello World'); }).listen(3000); (Correct answer)
- http.newServer().on('request', () => 'Hello World').listen(3000);
- http.listen(3000, (req, res) => res.write('Hello World'));
Correct answer: http.createServer((req, res) => { res.writeHead(200); res.end('Hello World'); }).listen(3000);
You call `http.createServer()` with a callback that uses `res.writeHead()` and `res.end()` to send the response, then chain `.listen()` to start the server.
Question 2: Which Node.js stream type is used for reading data from a source?
- Writable
- Duplex
- Readable (Correct answer)
- Transform
Correct answer: Readable
A Readable stream is used to consume data from a source such as a file, HTTP request body, or stdin.
Question 3: What does the `pipe()` method do when used on a Node.js Readable stream?
- It converts the stream to a Buffer
- It connects the Readable stream output directly to a Writable stream input (Correct answer)
- It splits the stream into multiple parallel streams
- It pauses the stream until the destination is ready
Correct answer: It connects the Readable stream output directly to a Writable stream input
`readable.pipe(writable)` automatically forwards data chunks from the readable to the writable and handles backpressure.
Question 4: What is backpressure in the context of Node.js streams?
- A memory error caused by reading too large a file at once
- A signal from a Writable stream that it cannot accept more data until it drains (Correct answer)
- A Node.js error thrown when a pipe chain is too long
- The delay introduced by the event loop between stream chunks
Correct answer: A signal from a Writable stream that it cannot accept more data until it drains
Backpressure occurs when a Writable stream's internal buffer is full and returns `false` from `write()`, signaling the source to pause sending data.
Question 5: In Node.js, what does `Buffer.from('hello', 'utf8')` return?
- A string 'hello' encoded as a base64 string
- A Buffer containing the UTF-8 encoded bytes of 'hello' (Correct answer)
- A Uint8Array of UTF-16 code units
- A ReadableStream of the string characters
Correct answer: A Buffer containing the UTF-8 encoded bytes of 'hello'
`Buffer.from(string, encoding)` allocates a new Buffer containing the string encoded in the specified format (UTF-8 here).
Question 6: Which HTTP method does `req.method` return for a browser form submission using the default form method?
- 'PUT'
- 'GET' (Correct answer)
- 'POST'
- 'PATCH'
Correct answer: 'GET'
HTML forms default to `method="get"`, so `req.method` is `'GET'` unless the form explicitly specifies `method="post"`.
Question 7: What Node.js module would you use to parse the query string `?name=Alice&age=30` into a JavaScript object?
- url
- querystring (Correct answer)
- http
- path
Correct answer: querystring
The `querystring` module (or the `URLSearchParams` API from the `url` module) parses URL-encoded query strings into key-value objects.
What is the correct way to create a simple HTTP server in Node.js that responds with 'Hello World'?