-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9408a3a
commit 0a4d7f4
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# mw-pipes | ||
|
||
connect-compatible middleware connector, but with more elegant support for streams. | ||
|
||
allow better support for streams that modify the request or response. | ||
|
||
``` js | ||
var pipes = require('mw-pipes') | ||
, http = require('http') | ||
|
||
http.createServer( | ||
pipes( | ||
function (req, res, next) { | ||
var x = new XStream() | ||
var y = new YStream() | ||
|
||
req.pipe(x) | ||
y.pipe(y) | ||
|
||
next(null, x, y) | ||
}, | ||
... | ||
``` | ||
the properties of `req` and `res` will be copied onto x and y with getters/setters so that | ||
they will behave just like a regular `ServerRequest` and `ServerResponse` to downstream middleware. | ||
## Example | ||
the following would fail using `connect` | ||
``` js | ||
var http = require('http') | ||
, pipes = require('mw-pipes') | ||
, BufferedStream = require('morestreams').BufferedStream | ||
; | ||
|
||
http.createServer(pipes( | ||
function (req, res, next) { | ||
var b = new BufferedStream() | ||
req.pipe(b) | ||
// replace the req stream with a buffered stream. | ||
// data events will be buffered until the pipe method is called. | ||
next(null, b) | ||
}, | ||
function (req, res, next) { | ||
// this wouldn't work with connect. | ||
// the data events would get lost, | ||
setTimeout(function () { | ||
res.writeHeader(202) | ||
req.pipe(res) | ||
}, 1000) | ||
} | ||
)).listen(8080) | ||
``` |