javascript 未捕获的 ReferenceError:io 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17260345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: io is not defined
提问by CaptainCarl
I know this is an error talked about a lot in here. But after a few posts found via google it still didn't solve my problem.
我知道这是一个错误在这里讨论了很多。但是在通过谷歌找到一些帖子后,它仍然没有解决我的问题。
I get the above error when trying to include the io javascript in my index.html.
尝试在我的 index.html 中包含 io javascript 时出现上述错误。
I've tried:
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
And <script src="/socket.io/socket.io.js"></script>
我试过:
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
而且<script src="/socket.io/socket.io.js"></script>
And even tried switching the port to 3000. The problem still persists nonetheless.
甚至尝试将端口切换到 3000。但问题仍然存在。
I've tried restarting my app.js and reinstalling socket.io(Although it is in my package) with npm install socket.io
. I see that a socket.io folder is created in my node_modules folder.
我试过重新启动我的 app.js 并重新安装 socket.io(虽然它在我的包中)npm install socket.io
。我看到在我的 node_modules 文件夹中创建了一个 socket.io 文件夹。
I start my server as following:
我启动我的服务器如下:
var express = require('express');
var app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
app.listen(8080);
And my index also loads the latest jQuery from Google: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
我的索引还加载了来自 Google 的最新 jQuery: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
My terminal shows that socket.io is started when i restart my app.js:
我的终端显示当我重新启动 app.js 时 socket.io 已启动:
23 Jun 13:31:56 - [nodemon] starting `node app.js`
info - socket.io started
My index.html in total(I retyped/remodeled it from a tutorial: http://psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/)
我的 index.html 总共(我从教程中重新输入/改造它:http: //psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/)
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
var socket = io.connect();
socket.on('connect', function() {
socket.emit('adduser', prompt("Hoe heet je?"));
});
socket.on('updateChat', function(username, data) {
$('#conversation').append('<strong>' + username + '</strong> says: ' + data + '<br>');
});
socket.on('updateUsers', function(data) {
$('#users').empty();
$.each(data, function(k, v) {
$("#users").append('<div>' + k + '</div>');
});
});
$(document).ready(function() {
$('#datasend').click(function() {
//get the message and empty the input
var msg = $('#data').val();
$('#data').val('');
//Let the server execute sendchat along with the msg
socket.emit("sendChat", msg);
});
//ENTER key
$('#data').keypress(function(e) {
keyCode = (e.keyCode ? e.keyCode : e.which);
if (keycode == 13) {
$(this).blur();
$('#datasend').focus().click();
}
});
});
</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
<b>USERS</b>
<div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
<div id="conversation"></div>
<input id="data" style="width:200px;" />
<input type="button" id="datasend" value="send" />
</div>
回答by user568109
In your code, change this
在您的代码中,更改此
app.listen(8080);
to this
对此
server.listen(8080);