我最近正在研究一個使用socket.io,node.js和express.js的javascript。 這裏是服務器代碼。node.js TypeError:路徑必須是字符串。收到null
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);
}
})
});
});
我能夠成功地架設服務器,但是當我使用本地主機上的瀏覽器,我收到以下錯誤:
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)
我已經開發了這個使用不穩定的v0.5.8 windows二進制版本的節點。 但我目前正在運行4.4.5版本。
請幫幫我,我哪裏出錯了?
非常感謝。我會嘗試。我試圖單獨運行index.html文件,它運行順暢,但我會再次檢查 –