nodejs write stream to file

Solutions on MaxInterview for nodejs write stream to file by the best coders in the world

showing results for - "nodejs write stream to file"
Jana
01 Oct 2019
1var http = require('http');
2var fs = require('fs');
3
4http.createServer(function(req, res) {
5  // This opens up the writeable stream to `output`
6  var writeStream = fs.createWriteStream('./output');
7
8  // This pipes the POST data to the file
9  req.pipe(writeStream);
10
11  // After all the data is saved, respond with a simple html form so they can post more data
12  req.on('end', function () {
13    res.writeHead(200, {"content-type":"text/html"});
14    res.end('<form method="POST"><input name="test" /><input type="submit"></form>');
15  });
16
17  // This is here incase any errors occur
18  writeStream.on('error', function (err) {
19    console.log(err);
20  });
21}).listen(8080);
22