Home  >  Article  >  Web Front-end  >  How to determine whether the downloaded network file exists in nodejs

How to determine whether the downloaded network file exists in nodejs

PHPz
PHPzOriginal
2023-04-06 09:11:50622browse

In Node.js, downloading network files is a very common operation. We can use the built-in module of Node.js to achieve this operation. But before implementing it, we need to determine whether the network file exists. This requires using the HTTP module of Node.js to detect the status of network files.

The HTTP module is one of the standard libraries of Node.js, which provides a set of APIs to communicate with HTTP and HTTPS servers. We can use the HTTP module to send requests to the server and obtain response data. Before downloading network files, we must first understand how the HTTP module detects the status of network files.

  1. HTTP module detects network file status

In Node.js, we can use the HTTP module to send a HEAD request to the server to obtain the metadata information of network files. This metadata information includes file size, last modification time, etc. Through this information, we can determine whether the network file exists and obtain relevant information about the network file. The following is a sample code that uses the HTTP module to detect the status of network files:

const http = require('http');
const url = 'http://example.com/sample.txt';

http.request(url, { method: 'HEAD' }, (res) => {
  console.log(res.statusCode);
})
.on('error', (err) => {
  console.error(err);
})
.end();

In the above code, we first introduce the HTTP module and the URL of the network file to be detected. Then, use the http.request() method to send a HEAD request to the server and obtain the server response. After getting the response, we can get the response status code through the res.statusCode property. If the response status code is 200, it means that the network file exists; if the response status code is 404, it means that the network file does not exist. If an error occurs during sending the request, you can capture the error through the .catch() method and print the error information.

  1. Detect and download network files

After determining whether the network file exists, we can use the built-in module fs of Node.js to implement the download operation.

const http = require('http');
const fs = require('fs');
const url = 'http://example.com/sample.txt';
const filePath = './sample.txt';

http.request(url, { method: 'HEAD' }, (res) => {
  if (res.statusCode === 200) {
    const file = fs.createWriteStream(filePath);
    http.get(url, (res) => {
      res.pipe(file);
    });
  } else {
    console.log('File not found.');
  }
})
.on('error', (err) => {
  console.log(err);
})
.end();

In the above code, we first use the HTTP module to send a HEAD request to the server to obtain the status code of the network file. If the status code is 200, it means that the network file exists. Then, we create a Writable Stream (fs.createWriteStream()) and send a GET request to the server through the http.get() method to obtain the network file content. After getting the file contents, we write the file to the local file system (res.pipe(file)).

If the network file does not exist, "File not found." will be output. If an error occurs while sending the HTTP request, an error message will be displayed.

Summary

In Node.js, you can use the HTTP module to detect the status of network files. You can use the HTTP module to send a HEAD request to the server to obtain metadata information about network files. After obtaining the status of the network file, we can use the built-in module fs of Node.js to implement the download operation.

The above is the detailed content of How to determine whether the downloaded network file exists in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn