I got over the hump with the new version (0.5) of fab.js. Tonight I would like to sanity check myself with what Jed describes as the very simplest v0.5 (fab) apps.
Again tonight, I will be using a fairly simple skeleton (fab) application:
// Don't crash on errorsThe
process.on('uncaughtException', function (err) {
console.log('Runaway exception: ' + err.stack);
});
with ( require('fab') )
( fab )
// Listen on the FAB port
( listen, 0xFAB )
(prepend_hello)
(simplest_app)
(prepend_hello)
()
();
function prepend_hello(write) {
return function read(body) {
return write(" hello ")(body);
};
}
prepend_hello()
(fab) app will prepend the text " hello " to the current stream, which ought to aid in seeing what is happening.First up, the simplest possible app, which should pass the stream back downstream:
function simplest_app(write) {When I access the server via
return write;
}
curl
, I see:cstrom@whitefall:~$ curl http://localhost:4011/ -d FooAh, interesting. I would have expected the write to short-circuit the stream, ignoring the last
hello hello
prepend_hello
. Instead it does nothing—connecting the two streams, making no changes.Next, I replace the
simplest_app
with the second simplest app:function second_simplest_app(write) {Accessing the server via
return write( "foo" );
}
curl
, I see:cstrom@whitefall:~$ curl http://localhost:4011/ -d FooGiven the behavior of the simplest app, the behavior of the second simplest app is to be expected.
hello foo hello
Last up tonight, I wonder how I might grab downstream body data. Ultimately, I am stumped. I can obtain the request header info with:
function read_from_downstream(write) {But I am stumped how to re-read from the downstream app so that I could access the request body. From
return write( function read(write, obj, hunh) {
if (obj.body) return write(obj.body);
return write(obj.url.pathname);
});
}
curl
, I get:curl http://localhost:4011/ -d FooUltimately, I am not too concerned. Fab is going to be changing quite a bit in the upcoming days, so this will get sorted out.
hello / hello
Day #221
chris,
ReplyDeletethe three parameters of a request in order are write, head, and body, where body is a stream.
function( write ) {
return write( function( write, head, body ) {
...
})
}
jed
Dammit! I *knew* that third argument was there and I knew it was a stream, but I didn't put 2 and 2 together. Gah!
ReplyDeleteSometimes I should stop exploring sooner rather than later.
Thanks for the pointer and all the help. Really jazzed for the new (fab) goodness now!