Skip to content

Allowing connect to be used as middleware #701

Merged
merged 3 commits into from Jan 13, 2013

3 participants

@guybedford

As in #697.

Let me know if you would like a test case as well.

This allows me to write the following code:

  var connect = require('connect'),
    http = require('http');

var myHandler = my_async_handler_generator();
var connectHandler = connect();

myHandler.on(function(req, res, next) {
  console.log('This handler comes before anything else');
  next();
});

myHandler.on(connectHandler);

myHandler.on(function(req, res, next) {
  res.writeHead(200, {
    'Content-Type': 'text/plain'
  });
  res.end('This is the next fall through after the connect handlers');
});

http.createServer(myHandler).listen(8080);
@tj
Sencha Labs member
tj commented Dec 6, 2012

test case would be good thanks

@guybedford

I've added in a test case for this. I've included it in server.js, but it may make more sense as a separate file.

@guybedford

It seems that the Travis build test doesn't like the following:

  http.get('http://localhost:5556/', function(res){
    //...
  });

The test was working fine on my localhost, suggestions on getting this working welcome.

@gregrperkins

Failing in node.js 0.6.21, per https://travis-ci.org/senchalabs/connect/jobs/3694390 .

This is because node's interface of http.get() changed from 0.6 to 0.8, per https://github.com/joyent/node/wiki/API-changes-between-v0.6-and-v0.8

See also:
http://nodejs.org/docs/v0.6.21/api/http.html#http_http_get_options_callback
http://nodejs.org/api/http.html#http_http_get_options_callback

Try replacing that line with:

      http.get({
        host: 'localhost',
        port: 5556,
        path: '/'
      }, function(res){
@guybedford

Thanks very much for the suggestion - I've added this in.

@guybedford

Just checking if this needs any further discussion or changes?

@tj tj merged commit 6ed44ac into senchalabs:master Jan 13, 2013

1 check passed

Details default The Travis build passed
@guybedford

Sweet, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.