用nodejs模拟服务器和前端网页;首先看下request.url里面是什么;下面前面三个var和一个http.crearSercer(functiaon(request,response){}是上来想都不用想,就要直接写出来的;

 

var http=require("http");
var url=require("url");
var fs=require("fs");
http.createServer(function(request,response ){
var urlObj=url.parse(request.url,true);
response.end(JSON.stringify(urlObj));
}).listen(8088);

结果树下:

 

    //rul输入的是:http://localhost:8088/abcd/e/g/zzzz.html?name=rose&age=22
/* {"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":"?name=rose&age=22","query":{"name":"rose","age":"22"},"pathname":"/abcd/e/g/zzzz.html","path":"/abcd/e/g/zzzz.html?name=rose&age=22","href":"/abcd/e/g/zzzz.html?name=rose&age=22"}*/

通过访问不同的目录展现不同的页面;

基本思路如下:

 

 

 

var http=require("http");
var url=require("url");
var fs=require("fs");
http.createServer(function(request,response ){
var urlObj=url.parse(request.url,true);
//response.end(JSON.stringify(urlObj));
// urlObj.pathname;//访问路径;
// urlObj.query;//查询字符;
var pathname=urlObj.pathname;
var iReg=/^\/.+\$/;
if(pathname=="/"){
fs.createReadStream("./default.html").pipe(response);
} else if(iReg.test(pathname)){
try{
fs.createReadStream("."+pathname+"default.html")
}catch (e){
response.writeHead(404);
response.end("error 404,nod found this find")
}
}else{
response.writeHead(404);
response.end("error 404,nod found this find")
}
}).listen(8088);