Reputation: 117
I have been working with node.js and socket.io, and following a tutorial, but I keep getting an error from an application I am building. This is my server.js file:
var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function(req, res) {
file.serve(req, res);
}).listen(1234);
var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket) {
function log() {
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function(message) {
log('Got message:', message);
socket.broadcast.emit('message', message);
});
socket.on('create or join', function(room) {
var numClients = io.sockets.clients(room).length;
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room ' + room);
if (numClients === 0) {
socket.join(room);
socket.emit('created', room);
} else if (numClients === 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + 'joined room' + room);
});
});
However when I browse to localhost:1234
I get the error Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:1234
Upvotes: 2
Views: 3132
Reputation: 1888
Put a index.html in you app root folder and it will be provided over 'node-static' as entrance file.
A more clean way is to configure a folder for all your frontend files.
var file = new static.Server('./public');
The full node-static docs can be found here: https://www.npmjs.com/package/node-static
Upvotes: 2