Skip to content
Browse files

Http exercise 3 - pg. 63

Writing request body (post) to a file using steams and pipes (very easy).

To use streams just create destination steam (method of fs in case of files)
and call the sourcestream.pipe (and source is request which is also a stream).
The piping takes care of verything.

Experienced some issues described in code.
  • Loading branch information...
1 parent 23edf51 commit 5beabe9f40365efda60eb93c645c00acd7e05aca Toon Ketels committed Dec 15, 2011
Showing with 65 additions and 0 deletions.
  1. +1 −0 files/posted.txt
  2. +1 −0 files/test-http-2-file-0.txt
  3. +1 −0 files/test-http-2-file-1.txt
  4. +1 −0 files/test3.txt
  5. +61 −0 http-3.js
View
1 files/posted.txt
@@ -0,0 +1 @@
+qsdfdqsfdqsdf
View
1 files/test-http-2-file-0.txt
@@ -0,0 +1 @@
+This is my first post, see if we can post faster.
View
1 files/test-http-2-file-1.txt
@@ -0,0 +1 @@
+This is my second post.
View
1 files/test3.txt
@@ -0,0 +1 @@
+This is my text ok?This is another request!I'm a posterchildNo, I am!
View
61 http-3.js
@@ -0,0 +1,61 @@
+// Goal: save the request body in file
+
+// Flow:
+// 1. Create server
+// 2. On request: create file to stream the data too
+// 3. Pipe the request data to the file stream
+
+// ISSUE:
+// Making a second post request takes a realy long time
+// before data is written to file. Something might be blocking
+// or this is a sideeffect of working with Poster.
+
+// First wrote everything to same file. Thought that this
+// would create this strange behavior. But writing to different
+// files results in the same latency. Even calling destroySoon
+// wont have any effect. @todo: investigate
+
+// ISSUE:
+// I was not able to write to the file using fs.open en fs.write.
+// This because the file system has some functioncallbacks and the
+// server has some function callbacks which I was unable to combine.
+// Looked at the solution and find out about streams, but doing it
+// manally should also be possible. @todo: investigate
+
+
+var fs = require( 'fs' );
+var sequence = 0;
+
+require( 'http' ).createServer( function(request, response ){
+ // Create a file
+ var path = __dirname + '/files/test-http-2-file-' + sequence + '.txt';
+ //var path = __dirname + '/files/test3.txt';
+ console.log( 'Creating file to write to on: ' + path );
+ // createWriteStream is a method of fs
+ var options = {
+ flags: 'a',
+ encoding: 'utf8'
+ }
+ var writeStream = fs.createWriteStream( path, options );
+
+ // Our request is also a stream, so we can call
+ // its stream methods
+ // Set the destination stream on the source stream using pipe
+ // This is all we need, the data gets pushed and closed automatically
+ request.pipe(writeStream);
+
+ // We are just login to log stuff on data
+ // and end events.
+ // Actual 'piping' of data is done through the pipe.
+ request.on( 'data', function( data ){
+ console.log( 'RECEIVED DATA: ' + data );
+ });
+ request.on( 'end', function(){
+ console.log( 'FINISHED WITH THIS REQUEST' );
+ response.writeHead( 200, {'Content-Type': 'text/plain' } );
+ response.end( 'Thank you!' );
+ });
+
+sequence ++;
+}).listen( 4000 );
+

0 comments on commit 5beabe9

Please sign in to comment.
Something went wrong with that request. Please try again.