Categories
JavaScript Answers

How to get HTTP headers with Node.js?

Spread the love

To get HTTP headers with Node.js, we use the headers property.

For instance, we write

const http = require("http");
const options = { method: "HEAD", host: "example.com", port: 80, path: "/" };
const req = http.request(options, (res) => {
  console.log(JSON.stringify(res.headers));
});
req.end();

to call http.request with the options to make a get request.

And we get the response headers with res.headers in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *