原生node.js路由页面跳转

//node 原生html加载css
//主要思想就是任何一个静态文件也应该做向应
// 
var http=require('http');
var fs=require('fs');
var server=http.createServer();
server.on('request',header);
function header(request,response) {
    var url=request.url;
    if (url === "/"){
        fs.readFile('index.html',function readData(err,data){
            response.writeHead(200,{'Content-Type':'text/html'});
            response.end(data)
        })
    }
    else if(url==='/about'){
        var surl='.'+url;
        var type=surl.substr(surl.lastIndexOf('.')+1,surl.length);
        response.writeHead(200,{'Content-Type':'text/'+type});
        fs.readFile(surl,function readData(err,data) {
            if(err){
                response.writeHead(404,{'Content-Type':"text/html"});
            }
            response.end(data)
        })
    }else if(url==='/index1'){
        var surl='.'+url;
        var type=surl.substr(surl.lastIndexOf('.')+1,surl.length);
        response.writeHead(200,{'Content-Type':'text/'+type});
        fs.readFile(surl,function readData(err,data) {
            if(err){
                response.writeHead(404,{'Content-Type':"text/html"});
            }
            response.end(data)
        })
    }
}
 server.listen('3030',function () {
    console.log('http://127.0.0.1:3030/')
 });

你可能感兴趣的:(原生node.js路由页面跳转)