Html5 provides a new Range tag to implement subsection downloads of files. In Node.JS, this label can be configured to download the file segments.

Header

Request Header: label request to download the file content

 range: bytes=3744-

3744 after the return Response Header: file length of 15522643 bytes of

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

Nginx

first configuration to configure Nginx supports range tag returns, simply add add_header Accept-Ranges bytes

; this line can be server {listen 80; server_name adksdf.com; location ^/ ~ (img/|js/|css/|upload/|font/|fonts/|res/|icon) Access-Control-Allow-Origin * add_header {add_header; Accept-Ranges bytes; root /var/www/; access_log off; expires M... Ax}

;}... After the opening, if node.js sends the request information containing range header, then the nginx will return with range related information:

this is a complete Response Header note, the total length of content-length here is not a file, but the length of range.

 {server:'nginx', date:'Wed, Jan 2018 02:43:20 24 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, Dec 2037 23:55:55 31 GMT','cache-control':'max-age=315360000','access-control-allow-origin'*,'accept-ranges':'bytes'.'content-range''bytes 3102456-15522642/15522643'}

: the total size can be obtained according to the header file in content-range.

Node.JS implements

. This example first detects the local half of the file, then creates the file flow in'r+'read and write mode, and writes the response flow to the file.

here adds the support of the form file to the range.

 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+'to create a 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 receives = var err = null var statusCode [] = res.statusCode var headers = res.headers var ws = fs.createWriteStream (filepat H 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) {})})} (

)... "Color:

> to return at the request of nginx may return to other status code, for example, the following 206 or 416, meaning:

206 Partial Content

is part of the contents of the documents returned to

416 Requested Range Not Satisfiable

range

file size request more than

. The above is small To introduce the Node.JS segment point http: Nginx configuration file section method download function, I hope to help you, if you have any questions please give me a message, Xiao Bian will reply you timely. Thank you for your support for the home website of the script!

This paper fixed link:http://www.script-home.com/node-js-point-http-nginx-configuration-file-download-function-realization-method-of-subsection.html | Script Home | +Copy Link

Article reprint please specify:Node.JS point: http Nginx configuration file download function realization method of subsection | Script Home

You may also be interested in these articles!