Node.js - 资源解释为脚本,但以MIME类型text / plain传输 [英] Node.js - Resource interpreted as Script but transferred with MIME type text/plain

查看:893
本文介绍了Node.js - 资源解释为脚本,但以MIME类型text / plain传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我没有使用Express。



不过,当我加载我的 index.html 文件时,它递归地 readFile 每个附加文件,例如我的CSS和JS页面。但它总是在我的检查器(Chrome)中返回这个错误:


资源解释为脚本,但以MIME类型传输text / plain


我完全不知道为什么这样做。这是我的代码:

  var http = require('http'); 
var querystring = require('querystring');
var fs = require('fs');
var url = require('url');

函数路由(句柄,路径名,响应,请求){
console.log(关于路由请求为+ pathname);
$ b $ if(typeof handle [pathname] ===function){
handle [pathname](response,request);
} else {

var file_path =;

//解析文件的url请求并获取路径名
var url_request = url.parse(request.url).pathname;
var tmp = url_request.lastIndexOf(。);
var extension = url_request.substring((tmp + 1));

file_path = url_request.replace(/,);

//载入需要的页面和静态文件
fs.readFile(file_path,function(error,contents){
if(error){
console.log 'DIE!');
console.log(error);
response.writeHeader(500,{Content-Type:text / html});
response.end( < h1> FS读取文件错误:内部服务器错误!< / h1>);
}
else {
console.log('SUCCESS!');
{//设置内容类型
if(extension ==='html')response.writeHeader(200,{Content-Type:'text / html'});
else if(extension = =='htm')response.writeHeader(200,{Content-Type:'text / html'});
else if(extension ==='css')response.writeHeader(200,{ Content-Type:'text / css'});
else if(extension ==='js')response.writeHeader(200,{Content-Type:'text / javascript'});
else if(extension ==='png')响应e.writeHeader(200,{Content-Type:'image / png'});
else if(extension ==='jpg')response.writeHeader(200,{Content-Type:'image / jpg'});
else if(extension ==='jpeg')response.writeHeader(200,{Content-Type:'image / jpeg'});
else {console.log(NO CORRECT EXTENSION)};
console.log(扩展名);
response.end(contents);
}

response.end();
});
response.end();
}
}

exports.route = route;

我该如何解决这个问题?这使我的项目停滞不前。



注意:这是我在此讨论过的早期问题的一个进展: Node.js不正确的路径问题

解决方案

尝试注释掉最后一个response.end。不要以为你需要它。

  response.end(); 
});
// response.end();
}
exports.route = route;

我怀疑response.end是在fs.readfile回调函数有时间之前立即调用的执行,也许关闭服务器上的标题,而不是给予正确的标题Content-Type:'text / javascript'的机会发送..



试过它在你的代码上,为我工作。


First off: I'm not using Express.

With that out of the way, when I load my index.html file it recursively readFile every attached file such as my CSS and JS pages. But it always returns this error in my inspector (Chrome):

Resource interpreted as Script but transferred with MIME type text/plain

I have absolutely no idea why it is doing this. Here is my code:

var http = require('http');
var querystring = require('querystring');
var fs = require('fs');
var url = require('url');

function route(handle, pathname, response, request){
console.log("About to route a request for " + pathname);

if (typeof handle[pathname] === "function"){        
    handle[pathname](response, request);
    } else {

    var file_path = "";

    // parses the url request for a file and pulls the pathname
    var url_request = url.parse(request.url).pathname;      
    var tmp  = url_request.lastIndexOf(".");
    var extension  = url_request.substring((tmp + 1));

    file_path = url_request.replace("/", "");

    //load needed pages and static files
    fs.readFile(file_path, function (error, contents){
        if(error){
          console.log('DIE!');
          console.log(error);
          response.writeHeader(500, {"Content-Type": "text/html"});  
          response.end("<h1>FS READ FILE ERROR: Internal Server Error!</h1>");    
        }
        else{ 
          console.log('SUCCESS!');
          // set content type
          if (extension === 'html') response.writeHeader(200, {"Content-Type": 'text/html'});
          else if (extension === 'htm') response.writeHeader(200, {"Content-Type": 'text/html'});
          else if (extension === 'css') response.writeHeader(200, {"Content-Type": 'text/css'});
          else if (extension === 'js') response.writeHeader(200, {"Content-Type": 'text/javascript'});
          else if (extension === 'png') response.writeHeader(200, {"Content-Type": 'image/png'});
          else if (extension === 'jpg') response.writeHeader(200, {"Content-Type": 'image/jpg'});
          else if (extension === 'jpeg') response.writeHeader(200, {"Content-Type": 'image/jpeg'});
          else { console.log("NO CORRECT EXTENSION")};
            console.log(extension);
            response.end(contents);
        }

        response.end();  
    });
    response.end();
}
}

exports.route = route;

How can I fix this? This has stopped my project in its tracks.

Note: this is a progression of an earlier problem I had talked about here: Node.js incorrect path problems

解决方案

Try comment out that last response.end. Don't think you need it.

    response.end();  
    });
    // response.end();
   }
   exports.route = route;

I suspect that response.end is getting called immediately before the one inside the fs.readfile callback has time to execute, maybe closing off the headers on the server not giving the correct header "Content-Type": 'text/javascript' a chance to get sent..

Tried it on your code, worked for me..

这篇关于Node.js - 资源解释为脚本,但以MIME类型text / plain传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆