Module “http”: What is http.createServer?
Creates a new HTTP server instance that handles requests via a callback.
Module “http”: What are req and res in http.createServer?
Instances of http.IncomingMessage and http.ServerResponse.
Module “http”: How do you start a server?
server.listen(port, hostname, callback)
Module “http”: How do you read request body?
Listen to ‘data’ and ‘end’ events on req stream.
Module “http”: Why is request body a stream?
To process data incrementally without loading entire payload into memory.
Module “http”: How do you send response?
Use res.write() and res.end().
Module “http”: What does res.end() do?
Finalizes and sends the response.
Module “http”: Difference between res.write and res.end?
write sends chunks, end finishes response.
Module “http”: What is res.writeHead?
Sets status code and headers in one call.
Module “http”: Difference between res.setHeader and res.writeHead?
setHeader queues headers, writeHead sends them immediately.
Module “http”: How to set status code?
res.statusCode = 200
Module “http”: What happens if res.write is called after res.end?
Throws an error because response is already finished.
Module “http”: How to stream a file to client?
fs.createReadStream(path).pipe(res)
Module “http”: What is http.request?
Used to make HTTP requests as a client.
Module “http”: Difference between http.request and http.get?
http.get is a simplified version for GET requests.
Module “http”: How do you send POST request?
Use http.request with method ‘POST’ and write body.
Module “http”: What is http.Agent?
Manages connection pooling and reuse of sockets.
Module “http”: How to enable keep-alive?
Use http.Agent with keepAlive: true.
Module “http”: What is req.socket?
Underlying TCP socket.
Module “http”: How to access headers?
req.headers
Module “http”: What event handles incoming data?
‘data’ event
Module “http”: What event signals request end?
‘end’ event
Module “http”: How to handle aborted request?
Listen to ‘aborted’ or ‘close’ events on req.
Module “http”: What is server.close()?
Stops the server from accepting new connections.