Request and Headers

A request is a message sent from a client, such as a web browser, to a server, asking for specific information or resources.

The headers are a collection of key-value pairs that provide additional information about the request. They contain metadata about the request, such as the type and format of the data being sent, the client's preferred language, and other details. Some common headers include "Content-Type", "User-Agent", "Accept-Encoding", and "Authorization".

For example, when a client sends a request to a server to retrieve a web page, the headers of that request might contain information such as the type of browser being used, the preferred language of the client, and any authentication credentials. The server uses this information to tailor its response to the specific needs of the client.

app.js

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.end('Hello World');
  console.log(req);
  console.log(res.getHeaders);
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Start the server with

node app.js

Now you can see the requests and headers.

Last updated