美文网首页
20160809周国康作业

20160809周国康作业

作者: ZGKzm | 来源:发表于2016-08-10 16:32 被阅读0次

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);
}
});

相关文章

网友评论

      本文标题:20160809周国康作业

      本文链接:https://www.haomeiwen.com/subject/ppdjsttx.html