When I use expressjs, but I don't know how to use it with streamlinejs.
Look at the express code:
var app = express.createServer();
app.get('/', function(req,res) {
User.find({name:'Jack'}, function(err, users) {
res.send(users);
});
});
How to use streamlinejs in the code?
That's a point I'm covering quickly in the FAQ (https://github.com/Sage/streamlinejs/blob/master/FAQ.md#the-underscore-trick-is-designed-for-callbacks-but-not-events-how-do-i-deal-with-events).
The easiest solution is to add the underscore as extra parameter to the get
callback:
app.get('/', function(req,res, next, _) {
var users = User.find({name:'Jack'}, _);
res.send(users);
});
The only problem would be if connect
adds a fourth parameter to its callback some day. The following is a bit safer (but probably overkill because I don't see why connect
would change its callback API):
app.get('/', function(req,res) {
(function(_) {
var users = User.find({name:'Jack'}, _);
res.send(users);
})(trap); // trap is a generic callback that handles errors
});
We use Express and Streamline for The Thingdom, and to do that, we built a simple wrapper that allows the next
callback (third parameter) to be a Streamline-friendly callback, for both route handlers and middleware handlers.
Here you go:
https://gist.github.com/1087991
=)
I've been meaning to make that into a generic module and publish to npm -- I'll do that soon. Edit: https://github.com/aseemk/express-streamline
Note that there is one trade-off as a result of this -- you can't have route handlers "continue" (fall through) to other route handlers. We've never used that feature, though, and don't foresee ourselves doing so -- it's much simpler to know that every route handler is the final destination -- so we were okay with that trade-off.
Edit: This is only tested with Express 2.x.