//引入http服务
var http = require(http);
//引入fs模块
var fs = require(fs);
//引入url模块
var url = require(url);
//引入path模块
var path = require(path);
//引入自定义模块
var mime = require(./static/module/getmime.js);
//创建服务
http.createServer((req,res) => {
//获取当前路径
var pathname = url.parse(req.url).pathname;
//获取文件的后缀名
var extname = path.extname(pathname)
//没有指定文件给出默认
if(pathname == /){
pathname = /index.html;
}
if(pathname != /favicon.ico){//过滤请求/favicon.ico
//读取文件
console.log(pathname)
fs.readFile(static+pathname,(err,data) => {
if(err){
//读取文件错误展示404页面
fs.readFile(static/html/404.html,(error,data404) => {
//文件写入头
res.writeHead(404,{"Content-Type":"text/html;charset=utf-8"});
//文件写入
res.write(data404);
//结束响应
res.end();
})
}else{
//获取文件类型
var mimeModule = mime.getmime(extname);
console.log(mimeModule)
//文件写入头
res.writeHead(200,{"Content-Type":""+mimeModule+";charset=utf-8"});
//文件写入
res.write(data);
//结束响应
res.end();
}
})
}
}).listen(8080)