Home > Web Front-end > JS Tutorial > body text

Node.JS implements segmented downloading of configuration files for segmented resume downloads

php中世界最好的语言
Release: 2018-04-11 15:22:15
Original
2291 people have browsed it

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-
Copy after login

Return Response Header: Total file length 15522643 bytes

accept-ranges': 'bytes'
content-range': 'bytes */15522643'
Copy after login

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;
 }
 ...
}
Copy after login

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' }
Copy after login

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()
 ...
})
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template