简体   繁体   English

Chrome说“资源解释为样式表,但使用MIME类型text / html传输”

[英]Chrome says “Resource interpreted as Stylesheet but transferred with MIME type text/html”

I'm trying to set up a simple chat using node.js (no express) and socket.io. 我正在尝试使用node.js(no express)和socket.io建立一个简单的聊天。 Problem is that Chrome gets stuck on my external includes in the html file, resulting in those files never getting included. 问题是Chrome卡在html文件中的外部包含上,导致这些文件永远不会被包含在内。 I'm including a ccs file, and three javascript files. 我包括一个ccs文件和三个javascript文件。 As suggested in answers to other related questions here on Stackoverflow, I checked my document's MIME type by requiring the mime module and using the mime.lookup(url) , which says 'text/html'. 正如在Stackoverflow上对其他相关问题的回答中所建议的那样,我通过要求mime模块并使用mime.lookup(url)来检查我的文档的MIME类型,其中显示了'text / html'。 I specifically set the returning header to 'Content-Type' : 'text/html' and even played around with setting it as 'text/css' and 'text/javascript' to no avail. 我特意将返回标题设置为'Content-Type' : 'text/html' ,甚至将其设置为'text / css'和'text / javascript'无效。 As of now I have no idea what to try next. 截至目前,我不知道下一步该尝试什么。 Please help! 请帮忙!

chat.html: chat.html:

<!DOCTYPE html>
    <html>
        <head>
        <title>CHAT</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <link type="text/css" rel="stylesheet" href="css/style.css" /> <!-- First line that Chrome complains about -->
        <script type="text/javascript" src="/socket.io/socket.io.js"></script> <!-- Second line that Chrome complains about -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
        <script type="text/javascript" src="chatClient.js"></script>
                    </head>
        <body>
        <h1>this shall be a chat</h1>
        </body>
    </html>

chatClient.js: chatClient.js:

var socket = io.connect('http://localhost');

        var chatBox = document.createElement('div');

        chatBox.id = 'chatBox';

        socket.on('server', function (data) {
        console.log('Server says: ', data.message);

        socket.emit('client', { clientMessage : 'this is all I have to say, right now!' });
});

chatServer.js: chatServer.js:

var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
mime = require('mime');

var url = __dirname + '/chat.html';
var mimeType = mime.lookup(url);

console.log(mimeType);

app.listen(8080);

function handler (req, res) {
    fs.readFile(url, function (err, data) {
    if (err) {
        res.writeHead(500);
        return res.end('Error loading chat');
    }

    res.setHeader('Content-Type', mimeType); // Sets the header to 'text/html'
    res.writeHead(200);
    res.end(data);
    });
}

io.sockets.on('connection', function (socket) {
    console.log('CHAT ONLINE');
    socket.emit('server', { message: 'hello world' });

    socket.on('client', function (data) {
    console.log('Client says: ', data.clientMessage);
    });
});

This is my absolute first time posting here so please let me know if there's anything more I should have included to assist in your helping me with this problem. 这是我第一次在这里发帖,所以请告诉我是否还有其他内容可以帮助我帮助解决这个问题。

You're setting mimeType during initialization based on one file that you might be sending out, rather than setting it based on the file that you're actually sending out. 您在初始化期间根据您可能发送的一个文件设置mimeType ,而不是根据您实际发送的文件进行设置。 That will cause any non-HTML files that you might send (eg CSS or JS files) to go out with a misleading Content-Type header. 这将导致您可能发送的任何非HTML文件(例如CSS或JS文件)带有误导性的Content-Type标头。 You need to do the check inside your request handler. 您需要请求处理程序中进行检查。

This has been answered before socket.io and differents folders --- solution found 这已在socket.io和不同文件夹---解决方案找到之前得到解答

I recommend to use connect or express framework. 我建议使用connect或express框架。 Makes your job easier. 让您的工作更轻松。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask:资源被解释为样式表,但使用 MIME 类型 text/html 传输 - Flask: Resource interpreted as Stylesheet but transferred with MIME type text/html 资源被解释为样式表,但在 AngularJS 中使用 MIME 类型 text/html 传输 - Resource interpreted as Stylesheet but transferred with MIME type text/html in AngularJS Firebase“资源解释为样式表,但使用 MIME 类型 text/html 传输” - Firebase "Resource interpreted as Stylesheet but transferred with MIME type text/html" Meteor Resource解释为样式表,但使用MIME类型text / html进行传输 - Meteor Resource interpreted as Stylesheet but transferred with MIME type text/html 资源被解释为样式表,但在 cloudflare 工作人员中使用 MIME 类型 text/html 传输 - Resource interpreted as Stylesheet but transferred with MIME type text/html in cloudflare workers 资源被解释为样式表,但使用 MIME 类型 text/javascript 传输 - Resource interpreted as Stylesheet but transferred with MIME type text/javascript 资源解释为样式表,但使用 MIME 类型 text/x-scss 传输 - Resource interpreted as Stylesheet but transferred with MIME type text/x-scss Chrome表示“资源被解释为脚本,但使用MIME类型的文本/纯文本传输。”,不带服务器 - Chrome says “Resource interpreted as script but transferred with MIME type text/plain.”, without server 角-干净的URL-资源解释为样式表,但以MIME类型text / html传输 - Angular - Clean URLs - Resource interpreted as stylesheet but transferred with MIME type text/html 流星@import css:资源解释为样式表,但以MIME类型text / html传输: - Meteor @import css: Resource interpreted as Stylesheet but transferred with MIME type text/html:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM