/*
* 这是fs模块
* */
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
http.createServer(function(req,res){
//得到用户的路径
var pathname = url.parse(req.url).pathname;
//默认首页
if(pathname == "/"){
pathname = "1.html";
}
//拓展名
var extname = path.extname(pathname);
//真的读取这个文件
fs.readFile("./static/"+pathname,function(err,data){
if(err){
//如果此页面不存在返回404
console.log(1);
fs.readFile("./static/404.html",function(err,data){
res.writeHead(404,{"Content-type":"text/html;charset=UTF-8"});
res.end(data);
});
return;
}
var mime = getMime(extname);
res.writeHead(200,{"Content-type":mime});
res.end(data);
});
}).listen(3000,"127.0.0.1");
function getMime(extname){
switch (extname){
case ".html":
return "text/html";
break;
case ".jpg":
return "image/jpg";
break;
case ".png":
return "image/png";
break;
case ".css":
return "text/css";
break;
}
}