nodeJS使用——示例1

 

//console.log("hello world");
var http=require("http");
var server=http.createServer(function(request,response){
    console.log(request.method);
    console.log(request.url);
    
    if(request.method==="GET"){
       response.setHeader("Content-Type","text/html");
       response.setHeader("Content-Encoding","UTF8");//UTF8编码
       var html="";//html代码
       switch(request.url){
            case "/":
            case "/index.html":
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                        "<title>标题</title></head><body>"+
                        "<h1>欢迎来到XX网</h1><br/>"+
                        "<a href=‘/add.html‘>ADD</a><br/>"+
                        "<a href=‘/remove.html‘>REMOVE</a><br/>"+
                        "<a href=‘/find.html‘>FIND</a><br/>"+
                        "<a href=‘/edit.html‘>EDIT</a><br/>"+
                        "</body></html>" 
                ;
            break;
            
            case "/add.html":
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                    "<title>标题</title></head><body>"+
                    "<h1>欢迎来到XX网</h1><br/>"+
                    "ADD UI界面..."+
                    "</body></html>" 
                ;
            break;
            
            case "/remove.html":
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                    "<title>标题</title></head><body>"+
                    "<h1>欢迎来到XX网</h1><br/>"+
                    "REMOVE..."+
                    "</body></html>" 
                ;
            break;
            
            case "/find.html":
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                    "<title>标题</title></head><body>"+
                    "<h1>欢迎来到XX网</h1><br/>"+
                    "FIND UI界面..."+
                    "</body></html>" 
                ;
            break;
            
            case "/edit.html":
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                    "<title>标题</title></head><body>"+
                    "<h1>欢迎来到XX网</h1><br/>"+
                    "EDIT UI界面..."+
                    "</body></html>" 
                ;
            break;
            
            default :
                html="<html><head><meta charset=‘UTF8‘/>"+  //UTF8编码
                    "<title>标题</title></head><body>"+
                    "<h1>404</h1><br/>"+
                    "REQUEST URL NOT FOUND..."+
                    "</body></html>" 
                ;
                console.log("Invalid request URL:"+request.url);
       
       };
        response.end(html);
    
    }
    response.end();

});

server.listen(80);

 

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