Reputation: 1146
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');
}
Upvotes: 1
Views: 1448
Reputation: 2864
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
Upvotes: 1