Node. JS segment resume: Implementation of the Nginx configuration file multipart download function, node. jsng.pdf

Source: Internet
Author: User

Node. JS segment resume: Implementation of the Nginx configuration file multipart download function, node. jsng.pdf

Html5 provides a new Range label for multipart object download. You can configure this label in Node. JS to implement multipart download of files.

Header label

Request Header: download the file content after 3744

range: bytes=3744-

Response Header: the total length of the file is 15522643 bytes.

accept-ranges': 'bytes'content-range': 'bytes */15522643'

Nginx Configuration

First, configure Nginx to support returned range tags. Simply add add_header Accept-Ranges bytes;

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 it is enabled, if the node. js end sends the request information containing the range header, nginx will return the information related to the range:

This is a complete Response Header. Note the following:content-length It 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' }

You can obtain the total File Size Based on the content-range in the header.

Node. JS implementation

This example detects half of the local files, creates a file stream in 'r + 'read/write mode, and writes the response stream to the file.

Here we will add range support 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 + '} // create the http object method 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 events es = [] 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 ()...})

Response Header

Another status code, such as 206 or 416, may be returned when requesting nginx. The meaning is as follows:

206 Partial Content

Partial file content is returned.

416 Requested Range Not Satisfiable

The requested range exceeds the file size.

Summary

The above is a small part of the Node. JS segment resume: the implementation method of the Nginx configuration file multipart download function. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.