本文介绍了SocketIO-GET http:// localhost:3000 / socket.io /?EIO = 3& transport = polling& t = 1418187395022-0 404(未找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有express的socketIO。



在我的项目中,我有一个登录页面和一个主页。
当我成功登录时,我导航到localhost:3000 / home,我收到这个错误:

  GET http: //localhost:3000/socket.io/?EIO=3&transport=polling&t=1418187395022-0 404(未找到)

我没有在我的app.js(由express创建的项目)中进行任何修改。



Index.js:

  var express = require('express'); 
var router = express.Router();
var http = require('http');
var fs = require('fs');
var io = require('socket.io')(http);
/ * GET主页。 * /
router.get('/',function(req,res){
res.render('index',{title:'Express'});
});

router.get('/ home',function(req,res){
res.render('home',{title:'Express'});
} );

io.on('connection',function(socket){
console.log(User Connected);
socket.on('chat message',function msg){
io.emit('chat message',msg);
console.log(Message);
});
socket.on('disconnect' ,function(msg){
console.log(User DisConnected);
});

});

router.post('/ authenticate',function(req,res){
fs.readFile(./ public / Verification / Users.json,utf8,function错误,数据){
if(err)
console.log(err);
else {
var result = JSON.parse(data);
for var i = 0; i< result.Users.length; i ++){
if(req.body.username == result.Users [i] .username&& req.body.password == result。用户[i] .password){
console.log(Success !!!!!!!!!!!!!);
res.location(home);
res.redirect(home);
}
}
}
});
});

module.exports = router;

当我浏览到localhost:3000 / home时,我会收到此错误。我是新来的socketIO和express。
请告诉我是否缺少某些东西。



谢谢



strong>:



在我的layout.jade中,我定义了这样的socketio:

  script(src ='https://cdn.socket.io/socket.io-1.2.0.js')


解决方案

如果您正在运行Express 4,在我看来,像是缺少代码行:

  var app = express(); 
var server = app.listen(3000);

这将启动您的Web服务器并将其设置为端口3000.看到骨骼简单的Express 4应用程序在这里:



然后,要启动socket.io,您将添加:

  var io = require('socket.io )。听(服务器); 

而且,不需要这一行:

  var http = require('http'); 


I am using socketIO with express.

In my project I have a login page and a home page.When i do successful login i navigate to localhost:3000/home where I get this error :

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=1418187395022-0 404 (Not Found)

I did not do any modification in my app.js(project created by express ).

Index.js :

var express = require('express');
var router = express.Router();
var http = require('http');
var fs = require('fs');
var io = require('socket.io')(http);
/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

router.get('/home', function(req, res) {
  res.render('home', { title: 'Express' });
});

io.on('connection', function(socket){
    console.log("User Connected");
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log("Message");
  });
   socket.on('disconnect', function(msg){
    console.log("User DisConnected");
  });

});

router.post('/authenticate', function(req, res) {
    fs.readFile("./public/Verification/Users.json", "utf8", function (err, data) {
        if (err)
            console.log(err);
        else{
            var result = JSON.parse(data);
            for(var i=0;i<result.Users.length;i++){
                if(req.body.username == result.Users[i].username && req.body.password ==     result.Users[i].password){
                    console.log("Success!!!!!!!!!!!!!!");
                    res.location("home");
                    res.redirect("home");
                }
            }
        }
        });
});

module.exports = router;

I get this error whenever I navigate to localhost:3000/home. I am new to both socketIO and express.Please tell me if I am missing something.

Thanks

EDIT:

In my layout.jade i defined socketio like this:

script(src='https://cdn.socket.io/socket.io-1.2.0.js')
解决方案

If you're running Express 4, it appears to me like you're missing the lines of code:

var app = express();
var server = app.listen(3000);

That will start your web server and set it to port 3000. See the bone simple Express 4 app here: http://expressjs.com/4x/api.html

And, then to start up socket.io, you would add:

var io = require('socket.io').listen(server);

And, there is no need for this line:

var http = require('http');

这篇关于SocketIO-GET http:// localhost:3000 / socket.io /?EIO = 3&amp; transport = polling&amp; t = 1418187395022-0 404(未找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 15:15