Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js HEAD http request

I want to just check if http file is available, how do I request only headers with Node.js?

http.head("http://domain.io", (res)=>{
   console.log(res.statusCode)
})
like image 617
m23 Avatar asked Mar 02 '19 20:03

m23


People also ask

How do I get request headers in node JS?

To get HTTP headers with Node. js, we can use the res. headers properties. const http = require("http"); const options = { method: "HEAD", host: "example.com", port: 80, path: "/", }; const req = http.

How do I use HTTP request in node JS?

Step to run the application: Open the terminal and write the following command. Approach 3 : Here we will send a request to updating a resource using node-fetch library. If you are already worked with Fetch in browser then it may be your good choice for your NodeJS server. Rewrite the index.

Is head a HTTP request?

HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

What is HTTP header in node JS?

The header tells the server details about the request such as what type of data the client, user, or request wants in the response. Type can be html , text , JSON , cookies or others.

How to perform an HTTP GET request in Node JS?

There are many ways to perform an HTTP GET request in Node.js, depending on the abstraction level you want to use. The simplest way to perform an HTTP request using Node.js is to use the Axios library: However, Axios requires the use of a 3rd party library.

How to add a header with a request using NodeJS Axios?

There are two ways we can easily add a header with a request using the npm Axios package and the npm request package. In this example, we will create an HTTP post request with headers example. so you can see both examples nodejs axios http request with headers and npm request module get example.

What is the HTTP module in Node JS?

The http module is available natively with Node.js; there is no additional installation required. The data is initially converted into a string using the stringify function. The HTTP options specify the headers, destination address, and request method type.

How do I run https code in Node JS?

As HTTPS is a standard Node.js module, there’s been no need for a package.json — I wish I could say this for some of my Node.js projects. You can run the code simply with node native-https.js, provided you named the file native-https.js. It should show an output like below:


Video Answer


1 Answers

You can use http.request():

'use strict';

const http = require('http');

http.request('http://example.com', { method: 'HEAD' }, (res) => {
  console.log(res.statusCode);
}).on('error', (err) => {
  console.error(err);
}).end();
like image 78
vsemozhebuty Avatar answered Oct 19 '22 12:10

vsemozhebuty