Error node.js Failed to load resource

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

Answers


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


Need Your Help

CakePHP 3: How to create custom model callbacks?

cakephp events callback cakephp-3.0 cakephp-3.1

Need good start point to create custom model callbacks. For specific part of application i can't use default cakephp lifecycle callbacks (beforeSave, afterSave,..) because table is large. In contro...

WildCard for Object<String> in Java6

java swing generics wildcard jcombobox

Please how to correct casting and remove warning