


Node.JS implements segmented downloading of configuration files for segmented resume downloads
This time I will bring you Node.JSrealize segmented downloading of the segmented resume transmissionconfiguration file, Node.JS implements segmented downloading of the segmented resumed transmission configuration file What are the precautions for segment downloading? The following is a practical case, let’s take a look.
Header Tag
Request Request Header: Download the file content after 3744
range: bytes=3744-
Return Response Header: Total file length 15522643 bytes
accept-ranges': 'bytes' content-range': 'bytes */15522643'
Nginx configuration
First, you need to configure Nginx to support range tag return. It is very simple to add add_header Accept-Ranges bytes; this line.
server { listen 80; server_name adksdf.com; location ~ ^/(img/|js/|css/|upload/|font/|fonts/|res/|icon) { add_header Access-Control-Allow-Origin *; add_header Accept-Ranges bytes; root /var/www/...; access_log off; expires max; } ... }
After enabling, if the node.js side sends request information containing range header, nginx will return range-related information:
This is a complete Response Header. Note that content-length
here is not the total length of the file, but the length of the current range.
{ server: 'nginx', date: 'Wed, 24 Jan 2018 02:43:20 GMT', 'content-type': 'application/zip', 'content-length': '12420187', 'last-modified': 'Tue, 16 Jan 2018 12:09:47 GMT', connection: 'close', etag: '"5a5deb8b-ecdb53"', expires: 'Thu, 31 Dec 2037 23:55:55 GMT', 'cache-control': 'max-age=315360000', 'access-control-allow-origin': '*', 'accept-ranges': 'bytes', 'content-range': 'bytes 3102456-15522642/15522643' }
The total size of the file can be obtained based on the content-range in this header.
Node.JS implementation
This example first detects half-downloaded files locally, then creates a file stream in 'r' read-write mode, and writes the response stream to the file.
Here, range support will be added to the statement file.
var reqOptions = { url: packageUrl, headers: {} } var filepath = '/path/to/your/part/file' var fileOptions = {} fs.stat(filepath, function(err, states) { if (states) { //Range: bytes=3744- reqOptions.headers['range'] = 'bytes=' + states.size + '-' fileOptions = { start: states.size, flags: 'r+' } } //创建 http 对象方法 var reqUrl = reqOptions.url var urlObj = url.parse(reqUrl) var options = { hostname : urlObj.hostname , port : urlObj.port , path : urlObj.pathname , headers : reqOptions.headers || {} } var req = http.request(options, function(res) { var receives = [] var err = null var statusCode = res.statusCode var headers = res.headers var ws = fs.createWriteStream(filepath, fileOptions) ws.on('error', function(e) { console.log('ws error', e) }) res.on('data', function(chrunk) { ws.write(chrunk) }) res.on('error', function(err) { ws.end() }) res.on('end', function() { ws.end() }) }) req.on('error', function(e) { cb && cb(e, null, {}) }) req.end() ... })
Return Header
When requesting nginx, other status codes may be returned, such as 206 or 416, with the following meaning:
206 Partial Content
What is returned is part of the file content
416 Requested Range Not Satisfiable
The requested range exceeds the file size
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to deal with the sub-routes not being displayed in the bottom navigation bar of vue.js
How to implement randomization in JS Switch WeChat ID
The above is the detailed content of Node.JS implements segmented downloading of configuration files for segmented resume downloads. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Recently, many Win10 system users want to change the user profile, but they don’t know how to do it. This article will show you how to set the user profile in Win10 system! How to set up user profile in Win10 1. First, press the "Win+I" keys to open the settings interface, and click to enter the "System" settings. 2. Then, in the opened interface, click "About" on the left, then find and click "Advanced System Settings". 3. Then, in the pop-up window, switch to the "" option bar and click "User Configuration" below.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Helm is an important component of Kubernetes that simplifies the deployment of Kubernetes applications by bundling configuration files into a package called HelmChart. This approach makes updating a single configuration file more convenient than modifying multiple files. With Helm, users can easily deploy Kubernetes applications, simplifying the entire deployment process and improving efficiency. In this guide, I'll cover different ways to implement Helm on Ubuntu. Please note: The commands in the following guide apply to Ubuntu 22.04 as well as all Ubuntu versions and Debian-based distributions. These commands are tested and should work correctly on your system. in U
