node.js - Express 4 + Socket.io = 404

标签 node.js sockets express-4

<分区>

几个小时以来,我一直在尝试让 socket.io 与 express 4 一起工作。 我开始关注 this给了我一个工作休息 api 的教程。

为了让 socket.io 工作,我使用了命令

npm install socket.io

这将 socket.io 添加到我的 node_modules 目录中。在此之后,我编辑了我的 app.js 以包含以下代码:

//LINE above var app = espress();
var http    =     require('http').Server(app);
var io      =     require("socket.io")(http);
io.on('connection',function(socket){  
    console.log("A user is connected");
});
// my routes etc

在我的 (jade) View 中,我添加了以下代码:

script(src="https://cdn.socket.io/socket.io-1.2.0.js")
script.
    var socket = io('http://localhost/');

现在,当我使用 npm start 运行时,我在浏览器和 npm 控制台中收到以下错误:

http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1427056664743-51 404 (not found)

据我所知,我在相关问题的几乎每个答案中都没有使用 express-generator,我做错了什么?

my complete app.js

我试过了 this : 但结果是端口 3000 已被使用的错误。

最佳答案

感谢答案 here 解决了它

var app = express();
var http = require( "http" ).createServer( app );
var io = require( "socket.io" )( http );
http.listen(8080, "127.0.0.1");


io.on('connection',function(socket){  
    console.log("A user is connected");
});

在客户端:

script(src='/javascripts/socket.io-1.2.0.js')
script.
    var socket = io.connect( "http://localhost:8080");

更改端口成功了(8080 而不是 3000)

关于node.js - Express 4 + Socket.io = 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199593/

相关文章:

node.js - nodejs-无法从辅助函数获取数据

javascript - 在 native Node 的回调中调用回调

c - 使用 epoll 处理多个 TCP 连接上的数据

php - PHP脚本套接字保持连接状态

java - android发送和接收TCP包

node.js - 路由后的 Node Express 4 中间件

node.js - 卡在中间件中的 console.log(JSON.stringify(req)) 上

node.js - Passport.js Google 策略不适用于 React 应用程序

node.js - 将 Bluebird 用于 Mongoose ,得到 ".bind is not a function"

javascript - 故障排除错误 : connect ECONNREFUSED in nodejs stream-adventure tutorial