简单的静态服务器接口
简单的静态服务器接口, 直接上代码.
exports.start = function() {
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");
var server = http.createServer(function (req, res) {
//res.setHeader('Connection', 'keep-alive');
//res.setHeader('Expires', 'Mon, 31 Dec 2012 23:59:59 GMT');
//res.setHeader('Cache-Control', 'max-age=31536000');
var pathname = __dirname + url.parse(req.url).pathname;
if(path.extname(pathname) == "") {
pathname += "/";
}
if(pathname.charAt(pathname.length - 1) == "/") {
pathname += "index.html";
}
path.exists(pathname, function(exists) {
if(exists) {
var type = {
".html": "text/html",
".htm": "text/html",
".js": "text/javascript",
".css": "text/css",
".ico": "image/x-icon",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".xml": "text/xml",
".json": "application/json",
".txt": "text/plain",
".pdf": "application/pdf",
".swf": "application/x-shockwave-flash"
};
res.writeHead(200, {"Content-Type": type[path.extname(pathname)]});
fs.readFile(pathname, function(err, data) {
res.end(data);
});
} else {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}).listen(8080, "127.0.0.1");
console.log("Server is running at http://127.0.0.1:8080/");
return server;
}
1 回复
好文。 遇到简单地传送静态html的时候,不用redirect了。