静态文件的读取是参照Jackson的文章 用NodeJS打造你的静态文件服务器
var http = require("http");
var fs = require("fs");
var url = require("url");
var path = require("path");
var mine = require("./mime");
http.createServer(function(req, res){
var pathname = url.parse(req.url).pathname;
var ext = path.extname(pathname) || "unknow";
fs.readFile(__dirname + pathname, "utf8", function(err, content){
if(!err){
res.writeHead(200, {
'Content-Type': mine.types[ext]
});
res.write(content);
res.end();
}
});
}).listen(8080, function(){
console.log("server started");
})
mime.js:
exports.types = {
".css" : "text/css",
".html" : "text/html",
".js" : "text/javascript",
".png" : "image/png",
".gif" : "image/gif",
".jpg" : "image/jpg",
".jpeg" : "image/jpeg",
".ico" : "image/x-icon",
"unknow" : "text/plain"
}
在我访问localhost:8080/index.html的时候html文件,js文件,json文件都没问题,但是图片的请求都抛出这样的错误:
Image corrupt or truncated: http://localhost:8080/static/images/black_che.png
Image corrupt or truncated: http://localhost:8080/static/images/black_ma.png
Image corrupt or truncated: http://localhost:8080/static/images/black_xiang.png
Image corrupt or truncated: http://localhost:8080/static/images/black_shi.png
Image corrupt or truncated: http://localhost:8080/static/images/black_jiang.png
Image corrupt or truncated: http://localhost:8080/static/images/black_pao.png
Image corrupt or truncated: http://localhost:8080/static/images/black_zu.png
Image corrupt or truncated: http://localhost:8080/static/images/red_pao.png
Image corrupt or truncated: http://localhost:8080/static/images/red_bing.png
Image corrupt or truncated: http://localhost:8080/static/images/red_che.png
Image corrupt or truncated: http://localhost:8080/static/images/red_ma.png
Image corrupt or truncated: http://localhost:8080/static/images/red_xiang.png
Image corrupt or truncated: http://localhost:8080/static/images/red_shi.png
Image corrupt or truncated: http://localhost:8080/static/images/red_shuai.png
这里对图片静态文件需要做什么额外处理吗?