nodeJS使用——示例2

 

/*引入需要的 module*/
var http=require("http");
var fs=require("fs");

var server=http.createServer(function(request,response){
    if(request.method==="GET"){
        switch(request.url){
            case "/":
            case "/index.html":
                fs.readFile("index.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                    }
                    response.writeHeader(200,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();
                });
            break;
            
            case "/add.html":
                fs.readFile("add.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                     } 
                    response.writeHeader(200,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();

                });
            
            break;
            
            case "/remove.html":
                fs.readFile("remove.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                     } 
                    response.writeHeader(200,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();

                });
            break;
            
            case "/edit.html":
                fs.readFile("edit.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                     } 
                    response.writeHeader(200,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();
                });
            break;
            
            case "/find.html":
                fs.readFile("find.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                     } 
                    response.writeHeader(200,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();
                });
            break;
            
            default :
                console.log("invalid  request URL :"+request.url);
                fs.readFile("404.html",function(err,data){
                    if(err){
                        console.log(err);
                        throw err;
                     } 
                    response.writeHeader(404,{"Content-Type":"text/html"});
                    response.write(data.toString());
                    response.end();
                });
        }
    }
});
server.listen(80);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。