2016-06-13 160 views
0

我最近正在研究一個使用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版本。

請幫幫我,我哪裏出錯了?

回答

0

從你粘貼的示例代碼有點難以分辨,因爲你的堆棧跟蹤引用了你的index.js中的第45行作爲問題的根源(​​),但是當我查看你的示例/粘貼代碼一個IDE,它沒有在那裏做任何事情。在腳本的較早部分中,唯一調用sendfile(在堆棧跟蹤中進一步引用)。

假設這是您的問題的位置,您應該console.log()或以其他方式調試您嘗試發送給訪問者的路徑/文件名的值。請注意,在Windows中,路徑分隔符是\而不是/。您應該使用path.join()來形成所提供文件的最終路徑。 ExpressJS示例說明了這一點:

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

非常感謝。我會嘗試。我試圖單獨運行index.html文件,它運行順暢,但我會再次檢查 –

相關問題