标签:监听 htm blog UI substring listen file ceshi content
啊啊啊啊啊啊啊啊啊啊啊啊啊啊,被node的fs坑了一下午,我又爬上来了,要坚强的笑着活下去,嗯,没毛病老铁。
let http = require(‘http‘); let fs = require(‘fs‘); let url = require(‘url‘); let server = http.createServer((request,response)=>{ var pathname = url.parse(‘http://127.0.0.1:3000/ceshi.html‘,true,true); //注意这个url fs.readFile(pathname.path.substring(1), ‘utf-8‘,function (err,data) { if(err){ response.writeHead(404,{ ‘Content-Type‘ : ‘text/html‘ }); }else{ response.writeHead(200,{ ‘Content-Type‘ : ‘text/html‘ }); //设置响应头 response.write(data.toString()); } response.end(); });
});
server.listen(3000,‘127.0.0.1‘,()=>{ console.log("本地服务器创建完成,监听端口为3000,请在浏览器在中复制此地址进行访问 http://127.0.0.1:3000"); });
标签:监听 htm blog UI substring listen file ceshi content
原文地址:http://www.cnblogs.com/qiaohong/p/7705321.html