Writing files in Node.js

前端 未结 19 2348
情歌与酒
情歌与酒 2020-11-21 11:57

I\'ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

19条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-21 12:20

    You can of course make it a little more advanced. Non-blocking, writing bits and pieces, not writing the whole file at once:

    var fs = require('fs');
    var stream = fs.createWriteStream("my_file.txt");
    stream.once('open', function(fd) {
      stream.write("My first row\n");
      stream.write("My second row\n");
      stream.end();
    });
    

提交回复
热议问题