Minimal HTTP server with the http module
knowing the raw http API beneath frameworks.
createServer with a request listener, set status and Content-Type, end the response, call listen on 3000.
forgetting res.end so the connection hangs.
WHAT THIS TESTS This checks fluency with the raw http module that frameworks wrap. Knowing it shows you understand what Express does under the hood and can debug at the protocol level.
A GOOD ANSWER COVERS You require the http module, call http.createServer and pass a request listener function that receives the request and response objects. Inside it you set the status code to 200, set a Content-Type header of text/plain so clients render it correctly, and call res.end with the body string. Calling res.end both writes the final chunk and signals that the response is complete, flushing it to the client. Finally you call server.listen with the port number, optionally passing a callback that logs once the server is bound and ready.
COMMON WRONG ANSWERS Forgetting res.end, which leaves the request hanging because Node never finalizes the response. Using res.send, which exists only in Express, not the core http module. Omitting the listen call so the server object is created but never accepts connections.
LIKELY FOLLOW-UPS How do you route different URLs without a framework. How do you read the request method and path from req. Why is Content-Type important and what happens if you omit it.
ONE CONCRETE EXAMPLE The complete script: const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!'); }); server.listen(3000, () => console.log('Listening on 3000'));. Every request, regardless of method or path, receives the same plain text reply with a 200 status, and the listen callback confirms the server is accepting traffic.
Read the original → nodejs.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.