Skip to content
tezvyn:
Menu

Creating a Basic HTTP Server in Node.js

Source: nodejs.orgEasyHow cards are made

A Node.js HTTP server is a listening post that waits for requests on a port and runs your code to reply. It's the foundation for any web service, from simple APIs to full apps. The footgun: forgetting response.end() leaves the client hanging indefinitely.

Why it exists

The built-in http module allows Node.js to handle web traffic directly, without needing an external web server like Apache or Nginx. Node's event-driven, non-blocking architecture is ideal for building efficient servers that can manage many concurrent connections.

The mental model

Think of an HTTP server as an object that listens for network connections on a specific port. When a client, like a browser, sends a request to that port, the server emits a 'request' event. You provide a single function, the request listener, that executes every time this event occurs to process the request and generate a response.

How it works

You use the http.createServer() method, passing it a callback function. This function receives two arguments: a request object (IncomingMessage) and a response object (ServerResponse). The request object contains details like the URL, HTTP method, and headers. You use the response object to send data back, setting the status code and headers with response.writeHead() and writing the body with response.write(). Critically, you must call response.end() to signal that the response is complete. Finally, you call server.listen() on the created server object to start listening on a port.

When to use it

Use the raw http module for lightweight, low-level servers where you need minimal overhead and full control. It's excellent for simple microservices, educational purposes, or as the foundation for a custom framework. It gives you direct access to the request and response streams.

When not to use it

Avoid the raw http module for complex applications. It lacks essential features like routing, middleware, and advanced error handling. For most production systems, frameworks like Express, Fastify, or Koa are better choices. They use the http module internally but provide a much more productive, higher-level API.

One canonical example

First, you require the 'http' module. Then, you call http.createServer(), providing a callback that takes req and res as arguments. Inside this callback, you set the response status code to 200 (res.statusCode = 200), set the content type header (res.setHeader('Content-Type', 'text/plain')), and send the body while closing the connection (res.end('Hello, World!')). Finally, you call server.listen(3000) to start the server on port 3000.

Interview question

Which action is crucial for a Node.js HTTP server to signal that a response has been fully sent to the client?

  • a.Calling server.listen() to activate the server on a port.
  • b.Setting the response.statusCode to a success code like 200.
  • c.Invoking response.end() on the response object.Correct
  • d.Using response.write() to send all data chunks to the client.
Why?

The card explicitly states that `response.end()` is critical to signal that the response is complete, otherwise the client will hang indefinitely. While `response.write()` sends data, it does not close the connection or mark the response as finished.

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 node.js — each one lists the topics its interview covers.

See open roles