第一个res.end():
const fs=require("fs");
const path=require("path");
const url=require("url");
let getExt=function (extname) {
var data=fs.readFileSync('./data/mine.json');
let obj=JSON.parse(data.toString());
return obj[extname];
}
exports.static=function (req,res,staticPath) {
var pathName=url.parse(req.url).pathname;
console.log(pathName);
console.log("1");
pathName=pathName=='/'?'/index.html':pathName;
var ext=path.extname(pathName);
if(pathName!='/favicon.ico'){
try{
console.log("3");
let data=fs.readFileSync('./'+staticPath+pathName);
if(data){
console.log("4");
let mime=getExt(ext);
res.writeHead(200,{'Content-Type':''+mime+';charset="utf-8"'});
res.write(data);
res.end("5");
}
}
catch(err){
console.log(err);
}
}
}
第二个res.end():
var http = require('http');
const routes=require("./module/routes");
const url=require("url");
http.createServer(function (req, res) {
routes.static(req,res,'static');
var pathname=url.parse(req.url).pathname;
if(pathname=='/login'){
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.end('登录');
}else if(pathname=='/register'){
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.end('注册');
}else if(pathname=='/admin'){
res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
res.end('后台管理');
}else{
console.log("2");
res.writeHead(404,{'Content-Type':'text/html;charset="utf-8"'});
res.end("404 not found!");
}
}).listen(3000);
调试台log的数字分别是1 3 4 2,且页面输出了第一个res.end()中的5。