Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

27 lines (21 sloc) 1.134 kb

Writable Streams

The function fs.createWriteStream() creates a writable stream in a very simple manner. After a call to fs.createWriteStream with the filepath, you have a writeable stream to work with. It turns out that the response (as well as the request) objects are streams. So we will stream the POST data to the file output. Since the code is simple enough, it is pretty easy just to read through it and comment why each line is necessary.

var http = require('http');
var fs = require('fs');

http.createServer(function(req, res) {
  // This opens up the writeable stream to `output`
  var writeStream = fs.createWriteStream('./output');

  // This pipes the POST data to the file

  // After all the data is saved, respond with a simple html form so they can post more data
  req.on('end', function () {
    res.writeHead(200, {"content-type":"text/html"});
    res.end('<form method="POST"><input name="test" /><input type="submit"></form>');

  // This is here incase any errors occur
  writeStream.on('error', function (err) {
Jump to Line
Something went wrong with that request. Please try again.