Currently, I have the following block of code:
net = require('net');
var clients = [];
net.createServer(function(s) {
clients.push(s);
s.on('data', function (data) {
clients.forEach(function(c) {
c.write(data);
});
process.stdout.write(data);//write data to command window
});
s.on('end', function() {
process.stdout.write("lost connection");
});
}).listen(9876);
Which is used to set up my Windows computer as a server and receive data from my linux computer. It is currently writing data to the command window. I would like to write the data into a text file to specific location, how do i do this?
Answer
Use the fs
module to deal with the filesystem:
var net = require('net');
var fs = require('fs');
// ...snip
s.on('data', function (data) {
clients.forEach(function(c) {
c.write(data);
});
fs.writeFile('myFile.txt', data, function(err) {
// Deal with possible error here.
});
});
No comments:
Post a Comment