const http = require('http');
const url = require('url');
const fs = require('fs');
const ip = '127.0.0.1';
const port = 3000;
var server = new http.Server();
server.listen(port,ip);
server.on('request',function(req,res){
var pathname = url.parse(req.url);
var patt1=new RegExp("css");//定义正则表达式
console.log(pathname);
//封装读取文件函数
function Write(HtmlName) {
fs.readFile(HtmlName,function(err,content){
if (err) {
console.log(err);
}else{
if (patt1.test(HtmlName)) {//政策表达式判断是否是css文件
res.writeHead(200,{'Content-Type':'text/css'});
} else {
res.writeHead(200,{'Content-Type':'text/html;charset-utf-8'});
}
res.write(content);
res.end();
}
});
}
switch (pathname.path) {
case ''||'/':
Write('./index.html');
break;
default:
Write('.'+pathname.path);
}
});