Python里面有个简单的命令python -m SimpleHTTPServer
,但不支持目录访问,所以自己捣鼓了一个。用了很久了,放这儿存档。
var http = require('http'), url = require('url'), path = require('path'), fs = require('fs'), util = require('util'), querystring = require('querystring');var rootpath = process.cwd();var MIMETYPES = { 'css' : 'text/css', 'htm' : 'text/html', 'html': 'text/html', 'js' : 'application/javascript', 'ico' : 'image/x-icon', 'jpg' : 'image/jpeg', 'png' : 'image/png', 'mp3' : 'audio/mp3', 'ogg' : 'audio/ogg', 'plist':'text/xml'};http.createServer(function(req, res) { if (req.method !== 'GET') { console.log(req.method, req.url); req.setEncoding("utf8"); req.addListener("data", function (postDataChunk) { //console.log(postDataChunk); console.log(querystring.parse(postDataChunk)); }); req.addListener("end", function () { res.writeHead(400); res.write('GET Only.'); res.end(); }); return; } if (req.url === '/favicon.ico') { console.log(req.headers); res.write(''); res.end(); return; } var request_time = process.hrtime()[1]; var filename = decodeURIComponent(path.join(rootpath, url.parse(req.url).pathname)); //console.log(req.url, url.parse(req.url), filename, rootpath); if ('/jsonp' === req.url.slice(0, 6)) { var callback = url.parse(req.url).search.split('jsonp=')[1]; res.write(callback + '({abc:123})'); res.end(); return; } fs.stat(filename,function(err,fstats){ if (err) { if ('.map' !== req.url.slice(-4)) { var rurl = url.parse(req.url); console.log('404', rurl.pathname); rurl.search && console.log(querystring.parse(rurl.search.slice(1))); } res.writeHead(404); res.write('Not Found'); res.end(); return; } if(fstats.isFile() && filename.indexOf(rootpath) == 0){ var ext = path.extname(filename).toLowerCase().slice(1); console.log("sending file %s ",filename, ext); res.writeHead(200, {'Content-Type': MIMETYPES[ext] || "application/octet-stream"}); fs.createReadStream(filename).pipe(res); }else{ res.writeHead(200, {'Content-Type': 'text/html'}); res.write("文件列表 文件列表
"); fs.readdir(filename,function(err,files){ if(files.length == 0 || err){ res.write("No files available."); }else{ var i, p; res.write("
文件名 | 大小 |
%s | %d kb |
参考链接: