Created
August 24, 2013 02:13
-
-
Save coolicer/6325640 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require("fs"), | |
| path = require('path'), | |
| http = require('http'), | |
| url = require('url'), | |
| zlib = require('zlib'), | |
| mime = require('./mime').types, | |
| config = require('./config'); | |
| var PORT = 9090; | |
| var server = http.createServer(function(req, res){ | |
| var pathname = url.parse(req.url).pathname; | |
| if (pathname.slice(-1) === "/") { | |
| pathname = pathname + config.Welcome.file; | |
| } | |
| var realPath = __dirname + path.join("/assets", path.normalize(pathname.replace(/\.\./g, ''))); | |
| if(pathname === '/favicon.ico'){ | |
| res.end(); | |
| return; | |
| } | |
| var pathHandle = function (realPath) { | |
| fs.stat(realPath, function (err, stats) { | |
| if (err) { | |
| res.writeHead(404, "Not Found", {'Content-Type': 'text/plain'}); | |
| res.write("This request URL " + pathname + " was not found on this server."); | |
| res.end(); | |
| } else { | |
| if (stats.isDirectory()) { | |
| realPath = path.join(realPath, "/", config.Welcome.file); | |
| pathHandle(realPath); | |
| } else { | |
| var ext = path.extname(realPath); | |
| ext = ext ? ext.slice(1) : 'unknown'; | |
| var contentType = mime[ext] || "text/plain"; | |
| res.setHeader("Content-Type", contentType); | |
| var lastModified = stats.mtime.toUTCString(); | |
| var ifModifiedSince = "If-Modified-Since".toLowerCase(); | |
| res.setHeader("Last-Modified", lastModified); | |
| if (ext.match(config.Expires.fileMatch)) { | |
| var expires = new Date(); | |
| expires.setTime(expires.getTime() + config.Expires.maxAge*1000); | |
| res.setHeader("Expires", expires.toUTCString()); | |
| res.setHeader("Cache-Control", "max-age=" + config.Expires.maxAge); | |
| } | |
| if (req.headers[ifModifiedSince] && lastModified == req.headers[ifModifiedSince]) { | |
| res.writeHead(304, "Not Modified"); | |
| res.end(); | |
| } else { | |
| var raw = fs.createReadStream(realPath); | |
| var acceptEncoding = req.headers['accept-encoding'] || ""; | |
| var matched = ext.match(config.Compress.match); | |
| if (matched && acceptEncoding.match(/\bgzip\b/)) { | |
| res.writeHead(200, "Ok", {'Content-Encoding': 'gzip'}); | |
| raw.pipe(zlib.createGzip()).pipe(res); | |
| } else if (matched && acceptEncoding.match(/\bdeflate\b/)) { | |
| res.writeHead(200, "Ok", {'Content-Encoding': 'deflate'}); | |
| raw.pipe(zlib.createDeflate()).pipe(res); | |
| } else { | |
| res.writeHead(200, "Ok"); | |
| raw.pipe(res); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| }; | |
| pathHandle(realPath); | |
| }).listen(PORT); | |
| console.log("server is running at port" + PORT); | |
| /*-------------------------config.js----------------------------------*/ | |
| exports.Expires = { | |
| fileMatch : /^(gif|png|jpg|js|css)$/ig, | |
| maxAge: 60*60*24*365 | |
| }; | |
| exports.Compress = { | |
| match: /css|js|html/ig | |
| }; | |
| exports.Welcome = { | |
| file: "index.html" | |
| }; | |
| /*-------------------------mime.js-----------------------------------*/ | |
| 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" | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment