nodejs不同浏览器跳转问题

前提:配置好简单的nodejs和bootstrap的服务,让用户可以通过localhost:8001和localhost:8001/index.html都可以访问

 1             fs.readFile("."+request.url,'utf-8',function(err,data)
 2             {
 3                 if(err)
 4                 {
 5                     throw err;
 6                 }
 7                 response.writeHead(200,{"Content-Type":{
 8                     ".html":"text/type",
 9                     ".css":"text/css",
10                     ".js" :"application/javascript"
11                 }
12                 });
13                 response.write(data);
14                 response.end();
15             })
16         

火狐:两个入口都可以访问,样式也正常加载了。

ie:localhost:8001/index.html可以正常访问,localhost:8001是下载index.html页面

chrome:两个入口都可以访问,但样式加载不了

解决办法:

 switch (ext)
    {
        case ".css":
        case ".js":
            fs.readFile("."+request.url,'utf-8',function(err,data){
                if(err)
                {
                    throw err;
                }
                response.writeHead(200,{"Content-Type":{
                    ".css":"text/css",
                    ".js" :"application/javascript"
                }[ext]
                });
                response.write(data);
                response.end();
            })
            break;
        default :
          fs.readFile("./index.html","utf-8",function(err,data)
                {
                    if(err) throw err;
                    response.writeHead(200,{"Content-Type":"text/html"});
                    response.write(data);
                    response.end();
                });
}
  
原文地址:https://www.cnblogs.com/hellowoody/p/3961956.html