MIME类型('text / html')错误

时间:2018-07-05 02:21:57

标签: javascript socket.io

因此,我尝试将require.js添加到我正在使用jQuery和Socket.io创建的项目中,但是我一直收到此错误。 enter image description here

我想知道这里发生了什么?我正在这样添加我的代码

    <script src="js/require.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    jQuery(function ($) {
        var socket = io.connect();
        var $nickForm = $('#setNick');
        var $nickError = $('#nickError');
        var $nickBox = $('#nickname');
        var $users = $('#users');
        var $messageForm = $('#send-message');
        var $messageBox = $('#message');
        var $chat = $('#chat');

        $nickForm.submit(function (e) {
            e.preventDefault();
            socket.emit('new user', $nickBox.val(), function (data) {
                if (data) {
                    $('#nickWrap').hide();
                    $('#contentWrap').show();

                } else {
                    $nickError.html('Name is taken! Choose a New one!');
                }

            });

            $nickBox.val('');

        });


        socket.on('usernames', function (data) {
            var html = '';

            for (var i = 0; i < data.length; i++) {
                html += data[i] + '<br/>'

            }

            $users.html(html);
        });

        $messageForm.submit(function (e) {
            e.preventDefault();
            socket.emit('send message', $messageBox.val(), function (data) {
                $chat.append('<span class = "error"' + data + "</span><br/>");

            });
            $messageBox.val('');

        });

        socket.on('new message', function (data) {
            $chat.append('<span class= "msg"<b>' + data.nick + ': </b>' + data.msg + "<span/><br/>");

        });

        socket.on('whisper', function (data) {
            $chat.append('<span class = "whisper"<b>' + data.nick + ': </b>' + data.msg + "</span><br/>");

        });
    });
</script>

编辑 这是我的服务器代码

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server),
    users ={};
    

server.listen(3000);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    res.sendFile(__filename + 'js/bower_components/crypto-js/crypto-js.js');
});



io.sockets.on('connection', function(socket){
    socket.on('new user', function(data, callback){
        if(data in users) {
            callback(false);
        } else{
            callback(true);
            socket.nickname = data;
            users[socket.nickname] = socket
            updateNames();
        }

    });

    function updateNames(){
        io.sockets.emit('usernames', Object.keys(users));
    }

    socket.on('send message', function(data, callback){
        var msg = data.trim();
        if(msg.substr(0,3) === '/w '){
            msg = msg.substr(3);
            var ind = msg.indexOf(' ');
            if(ind !== -1){
                var name = msg.substr(0, ind);
                var msg = msg.substr(ind + 1);
                if(name in users){
                    users[name].emit('whisper', {msg: msg, nick: socket.nickname});
                    console.log('hi');
                } else{
                    callback('Error! Enter a valid user!');
                }
            }else{
                callback("Error! Enter a pm");
            }
        } else {
            io.sockets.emit('new message', {msg: msg, nick: socket.nickname});
        }
    });

    socket.on('disconnect', function(data){
        if(!socket.nickname) return;
        delete users[socket.nickname];
        updateNames();    
        
    });
});

1 个答案:

答案 0 :(得分:0)

此错误基本上意味着js/require.js不包含有效的JS代码。

再次检查其是否不包含任何无关的代码或字符。