PATCH: Multiple listeners for http.createServer(...)

592 views
Skip to first unread message

Michael Stillwell

unread,
Feb 22, 2010, 7:21:20 PM2/22/10
to nod...@googlegroups.com
I created a small patch to make it possible to add multiple request
listeners via a single call to http.createServer(...).

i.e. with the patch

http.createServer(m1, m2).listen(PORT);

is equivalent to

var server = http.createServer(m1);
server.addListener("request", m2);
server.listen(PORT);

The patch (plus test case) is at

http://gist.github.com/311695

This makes simple HTTP middleware a little bit easier to build.

(I was thinking about whether it would make sense for the arguments to
http.createServer() to be applied in a chain, with execution halting
if a listener returned false, but I think it's probably better for
that behaviour to be implemented by hand.)

Cheers,
Michael

--
http://beebo.org

Mark Hansen

unread,
Feb 22, 2010, 8:44:41 PM2/22/10
to nod...@googlegroups.com
Sorry, I'm not sure I understand what this could be used for? Could
you give an example?

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Ryan Dahl

unread,
Feb 22, 2010, 10:27:55 PM2/22/10
to nod...@googlegroups.com
Hi Michael,

I think this API is good enough
var s = http.createServer();
s.addListener(m1);
s.addListener(m2);

Michael Stillwell

unread,
Feb 23, 2010, 3:25:28 AM2/23/10
to nod...@googlegroups.com

Oh, I didn't realise createServer() with no arguments would work. So the

server.addListener("request", null);

in http.js's createServer() will be a no-op? It seems as though
emitter.addListener("foo", something) doesn't do anything unless
typeof something == 'function'.

If you want it, there's a patch adding a test for the
s.addListener("request", m1), s.addListener("request", m2) style at

http://gist.github.com/311995

Michael

--
http://beebo.org

Michael Stillwell

unread,
Feb 23, 2010, 3:32:10 AM2/23/10
to nod...@googlegroups.com
> On Tue, Feb 23, 2010 at 1:21 PM, Michael Stillwell <m...@beebo.org> wrote:
>> I created a small patch to make it possible to add multiple request
>> listeners via a single call to http.createServer(...).

On Tue, Feb 23, 2010 at 1:44 AM, Mark Hansen <ma...@markhansen.co.nz> wrote:
> Sorry, I'm not sure I understand what this could be used for? Could
> you give an example?

Adding multiple request listeners is not tremendously useful (since
the request is largely unmodifiable and writing to the response at
multiple points is not really going to work), and no substitute for
real HTTP middleware, but you might want to use it to stack a logger
or ACL system, say, on top of your "regular" "request" listener.

Michael

--
http://beebo.org

Reply all
Reply to author
Forward
0 new messages