Skip to content
tezvyn:
Menu

Minimal HTTP server with the http module

Source: interviewEasyHow cards are made

Summary

knowing the raw http API beneath frameworks.

Key points

createServer with a request listener, set status and Content-Type, end the response, call listen on 3000.

Watch out for

forgetting res.end so the connection hangs.

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

In a bare http module server, what is the consequence of never calling res.end inside the request listener?

  • a.The client connection hangs because the response is never finalizedCorrect
  • b.Node automatically sends an empty 200 after a timeout
  • c.The request is silently retried by the http module
  • d.The server crashes with an unhandled exception
Why?

res.end finalizes and flushes the response; without it the client waits indefinitely. Node does not auto-complete the response, crash, or retry the request on its own.

Just read this? Test yourself on what you have been reading.

Read the original → nodejs.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on nodejs — each one lists the topics its interview covers.

See open roles