node. js static file Server Improved version

Source: Internet
Author: User

First of all, thanks to GitHub, thanks to the author of this section of the source on GitHub. It's a little bit more complicated than the static file server of the day, and you can learn a lot of new things.
You will find that this code is more than a fs.stat function and the Readstream object's pipe function, stat This function is used to obtain the file information. The first parameter is the pass-through file path, the second is the callback function, and the second parameter of the callback function, stats, is the basic information of the file. The pipe function is used to connect this readable stream with the destination target writable stream, and the data passed into the stream will be written to the destination stream. The source and destination streams are kept in sync by pausing and resuming the stream when necessary.

The improvement point of this static file server is that the last-modified and if-modified-since headers are used, and it is not necessary to return the file to the browser that it already exists. By the way, you can return to the resource for gzip or deflate compression based on the compression of the browser request resource.

varPORT = 8000;varHTTP = require ("http");varurl = require ("url");varFS = require ("FS");varPath = require ("path");varMIME = require ("./mime"). Types;varConfig = require ("./config"));varZlib = require ("Zlib");varServer = Http.createserver (function(Request, Response) {Response.setheader ("Server", "NODE/V5"); varpathname =Url.parse (request.url). Pathname; Console.log ("url =" +pathname); if(Pathname.slice (-1) = = = = "/") {Pathname= Pathname +CONFIG.    Welcome.file; }    varRealpath = __dirname + "/" + Path.join ("Assets", Path.normalize (Pathname.replace (/\.\./g, ""))); Console.log ("Realpath =" +Realpath); varPathhandle =function(Realpath) {Fs.stat (Realpath,function(err, stats) {if(Err) {Response.writehead (404, "Not Found", {' Content-type ': ' Text/plain '}); Response.Write ("Stats =" +stats); Response.Write ("This request URL" + pathname + "is not found on this server.");            Response.End (); } Else {                if(Stats.isdirectory ()) {Realpath= Path.join (Realpath, "/", CONFIG.                    Welcome.file);                Pathhandle (Realpath); } Else {                    varext =Path.extname (Realpath); Ext= ext? Ext.slice (1): ' Unknown '; varContentType = Mime[ext] | | "Text/plain"; Response.setheader ("Content-type", ContentType); //Get the file modification time                    varLastModified =stats.mtime.toUTCString (); varIfmodifiedsince = "If-modified-since". toLowerCase (); //Set last-modified                    //The server returns the file to the browser the last modified time last-modifiedResponse.setheader ("Last-modified", lastmodified); if(Ext.match (config. Expires.filematch)) {varexpires =NewDate (); Expires.settime (Expires.gettime ()+ CONFIG. Expires.maxage * 1000); Response.setheader ("Expires", expires.toutcstring ()); Response.setheader ("Cache-control", "max-age=" +CONFIG.                    Expires.maxage); }                    //the server receives the If-modified-since message header that the browser sends over                    //the same date indicates that the resource has not changed to return 304                    //tell the browser that the resource you already have, no need to request again                    if(Request.headers[ifmodifiedsince] && lastmodified = =Request.headers[ifmodifiedsince]) {Response.writehead (304, "Not Modified");                    Response.End (); } Else {                        varRaw =Fs.createreadstream (Realpath); varacceptencoding = request.headers[' accept-encoding ' | | ""; varmatched =Ext.match (Config.Compress.match); if(Matched && Acceptencoding.match (/\bgzip\b/) {response.writehead ($, "OK", {' content-encoding ': ' gzip '});                        Raw.pipe (Zlib.creategzip ()). pipe (response); } Else if(Matched && Acceptencoding.match (/\bdeflate\b/) {response.writehead ("OK", {' content-encoding ': ' Deflate '});                        Raw.pipe (Zlib.createdeflate ()). pipe (response); } Else{Response.writehead ($, "OK");                        Raw.pipe (response);    }                    }                }            }        });    }; Pathhandle (Realpath);}); Server.listen (PORT); Console.log ("Server runing at Port:" + Port + ".");


The Expires field declares the time when a Web page or URL address is no longer cached by the browser, and once the time has passed, the browser should contact the original server. The expiration time for this setting is 1 years.

Exports. Expires = {    /^ (gif|png|jpg|js|css) $/IG,    60*60*24*365= {    /css|js| html/= {    "index.html"};


Enumerates the types of various resources, which can be set according to the extension content-type.

Exports.types =  {  "CSS": "Text/css",  "GIF": "Image/gif",  "HTML": "Text/html",  "ico": "Image/x-icon",  "JPEG": "Image/jpeg",  "JPG": "Image/jpeg",  "JS": "Text/javascript",  "JSON": "Application/json",  "PDF": "Application/pdf",  "PNG": "Image/png",  "SVG": "Image/svg+xml",  "SWF": "Application/x-shockwave-flash",  "TIFF": "Image/tiff",  "TXT": "Text/plain",  "WAV": "Audio/x-wav",  "WMA": "AUDIO/X-MS-WMA",  "WMV": "Video/x-ms-wmv",  "xml": "Text/xml"};

node. js static file Server Improved version

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.