Tuesday, 2 May 2017

node socket IO can not connect error 404

I'm trying to create socket.io functionality for a simple school project. However I have made socket.io servers multiple times in the past. I can't figure out what is wrong even comparing my code to working code of my previous projects does not work out.

When my front end tries to connect it sais the following error:

http://localhost/socket.io/?EIO=3&transport=polling&t=Ll9SrWu 404 (Not Found)

My server.js file runs fine without errors. Al other modules work great of the node server but only the socket.io does not work. Code server.js file:

var express = require("express");
var app = express();
var server = require("http").createServer(app);
var io = require("socket.io")(server);
var fs = require("fs");

io.on("connection", function(client) {
    client.emit('connected', {
        connected: true
    });
});

var controllers = require('./controllers');
controllers.set(app,io,fs);

app.use(express.static('public'));

app.get("*",function(req,res){
  fs.createReadStream("./public/index.html").pipe(res);
});

app.listen(80);

I put the socket.io script in the server.js file but it used to be in the controllers folder. However if i remove the controllers folder lines completely this stil does not work. To be clear I removed these lines and it still fails.

var controllers = require('./controllers');
controllers.set(app,io,fs);

I tried to connect to a differend io server I made previously and it worked fine so it is a server issue however here is the code I use to connect.

var socket = io();
socket.on("connected",function(data){
    console.log("connected");
});



via Brecht Valcke

No comments:

Post a Comment