回答:我终于明白了,原来我也在错误的地方寻找记录器文件。它将它放在主文件夹中,而不是 javascript 所在的文件夹中。对我有用的代码是:
var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {flags: 'a', encoding: 'utf-8',mode: 0666});
server.listen(8888);
app.use("/", express.static(__dirname + '/files'));
io.sockets.on('connection', function (socket) {
socket.on('msg', function (data) {
io.sockets.emit('new', data);
log.on('error', function (err) {
console.log(err);
});
console.log(data);
log.on('error', function(e) { console.error(e); });
var newdata = JSON.stringify(data);
log.write(newdata += "\r\n")
});
});
我对 Node.js 完全陌生,所以我真的可以使用一些帮助。我正在尝试从我正在制作的聊天中记录消息到目前为止我有这个:
var fs = require('fs');
var log = fs.createWriteStream('nodelogger.txt', {'flags': 'a'});
server.listen(8888);
app.use("/", express.static(__dirname + '/files'));
io.sockets.on('connection', function (socket) {
socket.on('msg', function (data) {
io.sockets.emit('new', data);
log.write(data);
log.on('error', function (err) {
console.log(err);
});
});
});
但我不断收到此错误:
events.js:72
throw er; // Unhandled 'error' event
^
TypeError: Invalid non-string/buffer chunk
at validChunk (_stream_writable.js:150:14)
at WriteStream.Writable.write (_stream_writable.js:179:12)
at Socket.<anonymous> (/Users/test/Desktop/Chat/app.js:16:9)
at Socket.EventEmitter.emit [as $emit] (events.js:95:17)
at SocketNamespace.handlePacket (/Users/test/Desktop/Chat/node_modules/socket.io/lib/namespace.js:335:22)
at Manager.onClientMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/manager.js:488:38)
at WebSocket.Transport.onMessage (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transport.js:387:20)
at Parser.<anonymous> (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:39:10)
at Parser.EventEmitter.emit (events.js:95:17)
at finish (/Users/test/Desktop/Chat/node_modules/socket.io/lib/transports/websocket/hybi-16.js:288:16)
而且我不知道它是什么意思,也无法在任何地方找到它。有人知道我做错了什么吗?提前致谢!