1

I'm writing a chat application. In that, when the static file routing is working the socket.io (Chat) is not working throws not found error in console.

http://localhost/socket.io/?EIO=3&transport=polling&t=1486739955177-8 404 not found

When the chat is working fine then public static files is not working throws error

Cannot GET /public/index.html

The code chat working (public static files not working) :

var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
var path=require('path');

//Initialize application with route
app.get('/',function (req,res) {
 var express=require('express');
 app.use(express.static(path.join(__dirname+'/public')));
 res.sendFile(path.join(__dirname,'../public','chat.html'));
});

//Register events on socket connection
io.on('connection',function (socket) {
   socket.on('chatMessage',function (from, msg) {
    io.emit('chatMessage',from,msg);
 });

 socket.on('notifyUser',function (user) {
    io.emit('notifyUser',user);
  });
});

// Listen appliaction request on port 80
http.listen(80,function () {
   console.log('Server Running in port 80');
});

The code public static files working ( chat not working) :

var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
var path=require('path');

//Initialize application with route
var express=require('express');
app.use(express.static('public/'));
app.use('/public',express.static('public/stack'));


//Register events on socket connection
io.on('connection',function (socket) {
    socket.on('chatMessage',function (from, msg) {
        io.emit('chatMessage',from,msg);
    });
    socket.on('notifyUser',function (user) {
        io.emit('notifyUser',user);
    });
});


app.get('*', function(req, res){
    res.send('what???', 404);
});

// Listen appliaction request on port 80
app.listen(80,function () {
    console.log('Server Running in port 80');
}
7
  • I couldn't reproduce the error. My port 80 is busy with nginx, so I set it to 8000 and chat works with static as well. Commented Feb 10, 2017 at 16:24
  • GET http://localhost/socket.io/?EIO=3&transport=polling&t=1486743833778-61 404 (Not Found) no it's not working Commented Feb 10, 2017 at 16:25
  • that 404 is because of your wildcard route app.get('*', .. res.send('what???', 404); Commented Feb 10, 2017 at 16:28
  • I even commented that route. not working. In the network panel it's gives Cannot GET /socket.io/?EIO=3&transport=polling&t=1486744187480-26 Commented Feb 10, 2017 at 16:31
  • @SasiVarunan Same error.. Commented Feb 10, 2017 at 16:50

1 Answer 1

1

Ok this code works

var express=require('express');
var app = express();
var path=require('path');
var server = require('http').createServer(app);
var io=require('socket.io')(server);
//Initialize application with route
app.use(express.static('public/'));
// app.use('/public',express.static('public/stack'));

//Register events on socket connection
io.on('connection',function (socket) {
    socket.on('chatMessage',function (from, msg) {
        io.emit('chatMessage',from,msg);
    });
    socket.on('notifyUser',function (user) {
        io.emit('notifyUser',user);
    });
});

app.get('/', function(req, res){
    res.send('what???', 404);
});

// Listen appliaction request on port 80
server.listen(80,function () {
    console.log('Server Running in port 80');
});

Move your chat.html in side public folder and access like http://localhost/client.html

Directory structure is like

appdir public/client.html server.js node server.js

Sign up to request clarification or add additional context in comments.

2 Comments

It's working perfect. I want to know how to use socket functions other than server.js file. Because I want to store those msgs to database. If I write those functions here server.js file would look messy..
Then you need to write all your functions in separate js file and do export. Then in your server.js file you need to require it. Its worth reading and understanding exports and module.exports and dive in to your project sitepoint.com/understanding-module-exports-exports-node-js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.