节点js"错误:写完后#34;使用fs

时间:2018-06-06 15:24:01

标签: node.js fs

我在节点js中运行了一段代码,通过套接字读取传入的数据。信息显示在控制台上是有用的,但我也想将其写入文本文件以供进一步分析。 但是,由于我已经配置了代码,它工作但只是保留覆盖文件的内容。 我改变了它,试图确保文本文件不会被不断覆盖。我现在得到了#34;写完了#34;错误。我附上了下面的代码。如果变量"数据"中的原始数据我想要它。保存在文本文件中。这会很棒。 谢谢你们

Load the TCP Library

var net = require('net');
    var fs = require('fs');
// Keep track of the chat clients
var clients = [];
var tcpport = 13000;
var tcphost;
var wstream = fs.createWriteStream('myOutput.txt');
// Start a TCP Server
net.createServer(function (socket) {

  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort;

  // Put this new client in the list
  clients.push(socket);

  // Send a nice welcome message and announce
  console.log("Welcome " + socket.name);
  console.log(socket.name + " joined");

  // Handle incoming messages from clients.
  socket.on('data', function (data) {
    console.log('Incoming Data: ' + data.toString());


wstream.write(data.toString());

wstream.end();
    var str = data.toString().split("@");
    if (str[1] == 'Pong'){
        broadcast('AnnouncerKit@AckPong@$');
            broadcast('AnnouncerKit@Ping@$');
    }
    if (str[1]== 'AckPing'){
        broadcast('AnnouncerKit@GetInfo@$');
    }
    if (str[1]== 'Store'){
        broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
        //console.log(bibs);


    }
  });

  // Remove the client from the list when it leaves
  socket.on('end', function () {
    clients.splice(clients.indexOf(socket), 1);
    console.log(socket.name + " left");
  });

  // Send a message to all clients
  function broadcast(message) {
    clients.forEach(function (client) {
      client.write(message);
    });
    // Log it to the server output too
    console.log('Outgoing Data: ' + message);
  }

}).listen(tcpport);

console.log("TCP server running at port " + tcpport);
enter code here

另外仅供参考,这是工作代码,但它会覆盖。

// Load the TCP Library
var net = require('net');

// Keep track of the chat clients
var clients = [];
var tcpport = 13000;
var tcphost;

// Start a TCP Server
net.createServer(function (socket) {
        var fs = require('fs');
  // Identify this client
  socket.name = socket.remoteAddress + ":" + socket.remotePort;

  // Put this new client in the list
  clients.push(socket);

  // Send a nice welcome message and announce
  console.log("Welcome " + socket.name);
  console.log(socket.name + " joined");

  // Handle incoming messages from clients.
  socket.on('data', function (data) {
    console.log('Incoming Data: ' + data.toString());
    var wstream = fs.createWriteStream('myOutput.txt');
wstream.write(data.toString());

wstream.end();
    var str = data.toString().split("@");
    if (str[1] == 'Pong'){
        broadcast('AnnouncerKit@AckPong@$');
            broadcast('AnnouncerKit@Ping@$');
    }
    if (str[1]== 'AckPing'){
        broadcast('AnnouncerKit@GetInfo@$');
    }
    if (str[1]== 'Store'){
        broadcast('AnnouncerKit@AckStore@'+str[str.length-2]+'@$');
        //console.log(bibs);


    }
  });

  // Remove the client from the list when it leaves
  socket.on('end', function () {
    clients.splice(clients.indexOf(socket), 1);
    console.log(socket.name + " left");
  });

  // Send a message to all clients
  function broadcast(message) {
    clients.forEach(function (client) {
      client.write(message);
    });
    // Log it to the server output too
    console.log('Outgoing Data: ' + message);
  }

}).listen(tcpport);

console.log("TCP server running at port " + tcpport);

文本文件中的结果输出

Finish@Store@000064919:13:36.505 1 03  155641803211E@000089619:13:36.826 1 0F  155661803213D@683@$

1 个答案:

答案 0 :(得分:1)

在第二个片段中,使用:

var wstream = fs.createWriteStream('myOutput.txt', {flags: 'a'});
wstream.write(data.toString());
wstream.end();

以这种方式设置标志会为您提供一个写入文件末尾的编写器。有关其他可用选项,请参阅docs

在第一个代码段中,您将收到“write after end”错误,因为您在关闭(结束)后写入流。 writer.end()关闭了流。它将在第一次接收数据时工作,但是在套接字上写入的每个后续数据块上,您使用的是已经结束的同一个编写器。 在第二个代码段中,您不会收到此错误,因为在回调中,每次收到数据时都会重新创建流。 希望这会有所帮助。