0
votes

I was recently working on a javascript that uses socket.io,node.js and express.js. here is the server code.

var
    port = +process.argv[2] || 8080,

    sanitizer = require('validator').sanitize,
    express = require('express'),

    server = express.createServer(),
    io = require('socket.io').listen(server),
    chat = io.of('/chat'),
    canvas = io.of('/canvas')
;

function sanitize(string) {
    return sanitizer(string).entityDecode()
}

server.listen(port);

server.get(/(^\/.*$)/, function(request, response) {
    var fileName = request.params[0];
    if (fileName == '/')
        fileName = '/index.html';
    response.sendfile(__dirname + '/client' + fileName);
});

io.sockets.on('connection', function(socket) {
    socket.on('setName', function (name) {
        name = sanitize(name);
        socket.set('name', name);
        socket.broadcast.emit('receive', {
            sender:'Server',
            message:name + ' has joined.'
        })
    });

    socket.on('send', function (message) {
        socket.get('name', function(error, name) {
            if (name)
                socket.broadcast.emit('receive', {
                    sender:name,
                    message:sanitize(message)
                })
        })
    });

    socket.on('draw', function (command) {
        io.sockets.emit('draw', command)
    });

    socket.on('updateCursor', function(position) {
        socket.get('name', function(error, name) {
            if (name)
                socket.broadcast.emit('updateCursor', {
                    name:name,
                    position:position
                });
        });
    });

    socket.on('disconnect', function() {
        socket.get('name', function(error, name) {
            if (name) {
                socket.broadcast.emit('receive', {
                    sender:'Server',
                    message:name + ' has left.'
                });
                socket.broadcast.emit('removeCursor', name);
            }
        })
    });
});

I am able to set up the server successfully, but when I use the localhost on the browser, I get the following error:

TypeError: Path must be a string. Received null at assertPath (path.js:8:11) at win32.join (path.js:221:5) at exports.send (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\node_modules\connect\lib\middleware\static.js:127:20) at ServerResponse.res.sendfile (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\response.js:186:3) at C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\server.js:23:14 at callbacks (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:272:11) at param (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:246:11) at pass (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:253:5) at Router._dispatch (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:280:5) at Object.Router.middleware [as handle] (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:45:10)

I had developed this using the unstable v0.5.8 windows binary version of node. but I am currently running the 4.4.5 version.

Please help me, where did I go wrong?

1

1 Answers

0
votes

It's a little difficult to tell from the sample code you've pasted because your stack trace references line 45 in your index.js as the source of the problem (C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:45:10) but when I look at your sample/pasted code in an IDE, it's not doing anything there. The only call to sendfile (referenced further up in your stack trace) is earlier in the script.

Assuming that's the location of your issue, you should console.log() or otherwise debug the value of the path/filename you're attempting to send to the visitor. Note that in Windows, path delimiters are \ not /. You should use path.join() to form the final path to the file served. The ExpressJS examples illustrate this:

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname, 'index.html'));
});