我最近在使用socket.io,node.js和express.js的javascript。 这是服务器代码。
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);
}
})
});
});

我能够成功设置服务器,但是当我在浏览器上使用localhost时,出现以下错误:
TypeError:Path必须是字符串。收到null 在assertPath(path.js:8:11) 在win32.join(path.js:221:5) 在exports.send(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ node_modules \ connect \ lib \ middleware \ static.js:127:20) 在ServerResponse.res.sendfile(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ response.js:186:3) 在C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ server.js:23:14 在回调(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ router \ index.js:272:11) 在param(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ router \ index.js:246:11) 传递(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ router \ index.js:253:5) 在Router._dispatch(C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ router \ index.js:280:5) 在Object.Router.middleware [作为句柄](C:\ Users \ Akshay \ Desktop \ whiteboard-master \ whiteboard-master \ node_modules \ express \ lib \ router \ index.js:45:10)
我使用不稳定的v0.5.8 windows二进制版本的节点开发了这个。 但我目前正在运行4.4.5版本。
请帮助我,我哪里出错了?
答案 0 :(得分:0)
从您粘贴的示例代码中判断一下有点困难,因为您的堆栈跟踪引用了index.js中的第45行作为问题的来源(C:\Users\Akshay\Desktop\whiteboard-master\whiteboard-master\node_modules\express\lib\router\index.js:45:10
)但是当我查看您的示例/时在IDE中粘贴代码,它没有做任何事情。对sendfile
的唯一调用(在堆栈跟踪中进一步引用)在脚本中较早。
假设这是您问题的位置,您应该console.log()
或以其他方式调试您尝试发送给访问者的路径/文件名的值。请注意,在Windows中,路径分隔符为\
而不是/
。您应该使用path.join()
来形成所服务文件的最终路径。 ExpressJS的例子说明了这一点:
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});