nodeJS 静态文件服务器源码
2012-08-01 08:21
615 查看
一个简单的nodeJS的静态文件服务器,目前还没有加入gzip和缓存
这样就可以用了,可以先简单的用它做静态文件的访问使用
var server = require("http"), url = require("url"), path = require("path"), fs = require("fs"), mime = require("./mime").types; // 处理客户端的请求 server.createServer(function (request, response) { var pathname = url.parse(request.url).pathname; var realPath = "app" + pathname; path.exists(realPath, function (exists) { if (!exists) { response.writeHead(404, { 'Content-Type': 'text/plain' }); response.write("访问路径没有找到。"); response.end(); } else { fs.readFile(realPath, "binary", function (err, file) { if (err) { response.writeHead(500, { 'Content-Type': 'text/plain' }); response.end(err); } else { var ext = path.extname(realPath); ext = ext ? ext.slice(1) : 'html'; var contentType = mime[ext] || "text/plain"; response.writeHead(200, { 'Content-Type': contentType }); response.write(file, "binary"); response.end(); } }); } }); }).listen(9099); //控制台中输出信息 console.log("Server is listening port 9099...");另外还有一个mime.js用来支持MIME映射
/** * @author wangbin */ exports.types = { "aiff": "audio/x-aiff", "arj": "application/x-arj-compressed", "asf": "video/x-ms-asf", "asx": "video/x-ms-asx", "au": "audio/ulaw", "avi": "video/x-msvideo", "bcpio": "application/x-bcpio", "ccad": "application/clariscad", "cod": "application/vnd.rim.cod", "com": "application/x-msdos-program", "cpio": "application/x-cpio", "cpt": "application/mac-compactpro", "csh": "application/x-csh", "css": "text/css", "deb": "application/x-debian-package", "dl": "video/dl", "doc": "application/msword", "drw": "application/drafting", "dvi": "application/x-dvi", "dwg": "application/acad", "dxf": "application/dxf", "dxr": "application/x-director", "etx": "text/x-setext", "ez": "application/andrew-inset", "fli": "video/x-fli", "flv": "video/x-flv", "gif": "image/gif", "gl": "video/gl", "gtar": "application/x-gtar", "gz": "application/x-gzip", "hdf": "application/x-hdf", "hqx": "application/mac-binhex40", "html": "text/html", "ice": "x-conference/x-cooltalk", "ico": "image/x-icon", "ief": "image/ief", "igs": "model/iges", "ips": "application/x-ipscript", "ipx": "application/x-ipix", "jad": "text/vnd.sun.j2me.app-descriptor", "jar": "application/java-archive", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "text/javascript", "json": "application/json", "latex": "application/x-latex", "lsp": "application/x-lisp", "lzh": "application/octet-stream", "m": "text/plain", "m3u": "audio/x-mpegurl", "man": "application/x-troff-man", "me": "application/x-troff-me", "midi": "audio/midi", "mif": "application/x-mif", "mime": "www/mime", "movie": "video/x-sgi-movie", "mp4": "video/mp4", "mpg": "video/mpeg", "mpga": "audio/mpeg", "ms": "application/x-troff-ms", "nc": "application/x-netcdf", "oda": "application/oda", "ogm": "application/ogg", "pbm": "image/x-portable-bitmap", "pdf": "application/pdf", "pgm": "image/x-portable-graymap", "pgn": "application/x-chess-pgn", "pgp": "application/pgp", "pm": "application/x-perl", "png": "image/png", "pnm": "image/x-portable-anymap", "ppm": "image/x-portable-pixmap", "ppz": "application/vnd.ms-powerpoint", "pre": "application/x-freelance", "prt": "application/pro_eng", "ps": "application/postscript", "qt": "video/quicktime", "ra": "audio/x-realaudio", "rar": "application/x-rar-compressed", "ras": "image/x-cmu-raster", "rgb": "image/x-rgb", "rm": "audio/x-pn-realaudio", "rpm": "audio/x-pn-realaudio-plugin", "rtf": "text/rtf", "rtx": "text/richtext", "scm": "application/x-lotusscreencam", "set": "application/set", "sgml": "text/sgml", "sh": "application/x-sh", "shar": "application/x-shar", "silo": "model/mesh", "sit": "application/x-stuffit", "skt": "application/x-koan", "smil": "application/smil", "snd": "audio/basic", "sol": "application/solids", "spl": "application/x-futuresplash", "src": "application/x-wais-source", "stl": "application/SLA", "stp": "application/STEP", "sv4cpio": "application/x-sv4cpio", "sv4crc": "application/x-sv4crc", "svg": "image/svg+xml", "swf": "application/x-shockwave-flash", "tar": "application/x-tar", "tcl": "application/x-tcl", "tex": "application/x-tex", "texinfo": "application/x-texinfo", "tgz": "application/x-tar-gz", "tiff": "image/tiff", "tr": "application/x-troff", "tsi": "audio/TSP-audio", "tsp": "application/dsptype", "tsv": "text/tab-separated-values", "txt": "text/plain", "unv": "application/i-deas", "ustar": "application/x-ustar", "vcd": "application/x-cdlink", "vda": "application/vda", "vivo": "video/vnd.vivo", "vrm": "x-world/x-vrml", "wav": "audio/x-wav", "wax": "audio/x-ms-wax", "wma": "audio/x-ms-wma", "wmv": "video/x-ms-wmv", "wmx": "video/x-ms-wmx", "wrl": "model/vrml", "wvx": "video/x-ms-wvx", "xbm": "image/x-xbitmap", "xlw": "application/vnd.ms-excel", "xml": "text/xml", "xpm": "image/x-xpixmap", "xwd": "image/x-xwindowdump", "xyz": "chemical/x-pdb", "zip": "application/zip" };
这样就可以用了,可以先简单的用它做静态文件的访问使用
相关文章推荐
- 用NodeJS打造你的静态文件服务器
- 用NodeJS打造你的静态文件服务器 <转载>
- NodeJS VS Tomcat搭建高并发静态文件服务器
- nodejs使用nodejs创建简单的静态文件服务器
- 使用Nodejs快速搭建一个的本地静态文件服务器
- 用NodeJS打造你的静态文件服务器
- Nodejs实现web静态服务器对多媒体文件的支持
- nodejs入门-静态文件服务器
- 用Nodejs搭建服务器访问html、css、JS等静态资源文件
- nodejs静态文件服务器
- 使用nodejs、Python写的一个简易HTTP静态文件服务器
- 使用nodejs、Python写的一个简易HTTP静态文件服务器
- 用NodeJS打造你的静态文件服务器
- nodejs使用nodejs创建简单的静态文件服务器
- 一个简单的基于nodejs的静态文件服务器
- nodejs 静态文件服务器
- nodejs使用nodejs创建简单的静态文件服务器
- nodejs 创建静态文件服务器
- [Nodejs]十分钟快速编写简单静态文件服务器
- 在windows上用nodejs搭建静态文件服务器的简单方法