node.js GET http://localhost:3000/socket.io/socket.io.js 404(未找到)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16981396/
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
GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
提问by babbaggeii
I'm trying to stream data to the browser. I'm struggling, however, to connect it to the browser. Here's my html:
我正在尝试将数据流式传输到浏览器。但是,我正在努力将其连接到浏览器。这是我的 html:
<ul class="tweets"></ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
jQuery(function ($) {
var tweetList = $('ul.tweets');
socket.on('tweet', function (data) {
tweetList .prepend('<li>' + data.user + ': ' + data.text + '</li>');
});
});
</script>
And here's the relevant parts of my app.js:
这是我的 app.js 的相关部分:
var express = require('express')
, twitter = require('ntwitter')
, http = require('http')
, path = require('path');
var app = express();
var io = require('socket.io').listen(app);
app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); });
app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
io.sockets.volatile.emit('tweets', {
user: data.user.screen_name,
text: data.text,
geo : geo,
latitude: latitude,
longitude: longitude
});
I installed socket.io 0.9.16 via my packages.json file:
我通过我的 packages.json 文件安装了 socket.io 0.9.16:
"dependencies": {
"express": "3.2.6",
"jade": "*",
"ntwitter":"0.2.10",
"socket.io":"0.9.x"
}
Can anyone help me out here? Why can't it find the file?
有人可以帮我从这里出去吗?为什么找不到文件?
Digging a bit deeper. To test the socket, I put this in the app.js:
挖得更深一些。为了测试套接字,我把它放在 app.js 中:
var socket = io.listen(app);
And I get the error:
我得到错误:
TypeError: Object #<Manager> has no method 'listen'
at Object.<anonymous> (/home/andy/dev/node/mytwittermap/app.js:49:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
采纳答案by Milli
Can you try this:
你能试试这个吗:
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
I guess, you will have to instantiate the socket.io server.
我想,您必须实例化 socket.io 服务器。
回答by robertklep
Your setup needs to look something like this:
您的设置需要如下所示:
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
...
server.listen(app.get('port')); // not 'app.listen'!
回答by nico
- You need to instantiate the socket.io connection and
- You need to use
server.listen()and notapp.listen()
- 您需要实例化 socket.io 连接和
- 你需要使用
server.listen()而不是app.listen()
Try something like this:
尝试这样的事情:
// at the top of app.js
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
// your code
// at the bottom of app.js
server.listen('3000', () => {
console.log('Server listening on Port 3000');
})

