我正在运行一个使用 websockets 的 Node js 服务器,当我在 Windows 上运行它时,一切正常。它创建 websockets,发送消息拉起页面。
然而,我将我的代码转移到一台 linux 机器上,就像它在 Windows 上工作时一样,我得到了以下错误 -
Client websocket connected!
fs.js:297
binding.open(pathModule_makeLong(path),
TypeError: path must be a string or Buffer
这发生在我从这一行调用的 bindCallback 函数中 -
// Create the server object
var server = http.createServer(serverCallback);
// Setup the server to listen
server.listen(8080, bindCallback);
在绑定(bind)回调函数中我有这个 -
var bindCallback = function () {
websocket_server.on('connection', function (client_socket) {
console.log("Client websocket connected!");
client_socket.on('message', function (message) {
console.log("Received websocket greeting!: " + message);
});
acceptCallback(client_socket);
});
};
根据控制台输出,TypeError:path must be a string or Buffer error is occurring at the line that says -
acceptCallback(client_socket);
完整的错误输出如下所示 -
fs.js:297
binding.open(pathModule._makeLong(path),
^
TypeError: path must be a string or Buffer
at Object.fs.readFile (fs.js:297:11)
at acceptCallback (/home/pi/veud/nodejs/main.js:346:8)
at WebSocketServer.<anonymous> (/home/pi/veud/nodejs/main.js:397:9)
at emitTwo (events.js:106:13)
at WebSocketServer.emit (events.js:194:7)
at handleUpgrade (/home/pi/veud/nodejs/node_modules/ws/lib/WebSocketServer.js:88:16)
at WebSocketServer.completeUpgrade (/home/pi/veud/nodejs/node_modules/ws/lib/WebSocketServer.js:270:5)
at WebSocketServer.handleUpgrade (/home/pi/veud/nodejs/node_modules/ws/lib/WebSocketServer.js:197:10)
at Server.WebSocketServer._ultron.on (/home/pi/veud/nodejs/node_modules/ws/lib/WebSocketServer.js:87:14)
at emitThree (events.js:116:13)
知道为什么这个错误可能只发生在 Linux 而不是在 Windows 中以及如何修复它吗?
最佳答案
这可能是因为您传递的路径参数。 Windows 路径不区分大小写,但在 Linux 中,路径区分大小写。与反斜杠和正斜杠有关的差异更多。这些差异可能会产生问题。您可以阅读这篇讨论 windows 和 linux 路径差异的文章 - https://www.howtogeek.com/137096/6-ways-the-linux-file-system-is-different-from-the-windows-file-system/
关于node.js - Node js 服务器在 Windows 上运行时工作,但在 Linux 上运行时出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45443175/