nodejs服务器
/*** Created by HongxueYu on 2014/9/25.
*/
var http=require("http");
var url=require("url");
var fs=require("fs");
http.createServer(function(request,response){
var urlObj=url.parse(request.url);
console.log(urlObj);
//response.end('url parse');
var pathname=urlObj.pathname;
var query=urlObj.query;
if(pathname === "/"){
console.log(pathname);
//pathname='/index.html';
readFileAndResponse("/index.html",response);
}else if(pathname === '/ajax'){
response.end('{"msg":"this is a json response"}');
}else{
readFileAndResponse(pathname,response);
}
}).listen(8080);
function readFileAndResponse(pathname,response){
fs.readFile(pathname.substr(1),'utf-8',function(err,data){
if(err)
{
//console.log(err);
response.writeHead(404);
response.end("file not found");
}
response.end(data);
});
}
页:
[1]