Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allowing connect to be used as middleware #701

Merged
merged 3 commits into from

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
Owner
tj commented

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
@guybedford

Sweet, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 6, 2012
  1. @guybedford
Commits on Dec 16, 2012
  1. test connect as middleware

    guybedford authored
Commits on Dec 23, 2012
  1. nodejs 0.8 change as suggested

    guybedford authored
This page is out of date. Refresh to see the latest.
Showing with 41 additions and 2 deletions.
  1. +1 −2  lib/connect.js
  2. +40 −0 test/server.js
View
3  lib/connect.js
@@ -1,4 +1,3 @@
-
/*!
* Connect
* Copyright(c) 2010 Sencha Inc.
@@ -63,7 +62,7 @@ exports.utils = utils;
*/
function createServer() {
- function app(req, res){ app.handle(req, res); }
+ function app(req, res, next){ app.handle(req, res, next); }
utils.merge(app, proto);
utils.merge(app, EventEmitter.prototype);
app.route = '/';
View
40 test/server.js
@@ -45,6 +45,46 @@ describe('app', function(){
app.stack.should.have.length(3);
})
+ it('should work as middlware', function(done){
+ var http = require('http');
+
+ // custom server handler array
+ var handlers = [connect(), function(req, res, next){
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('Ok');
+ }];
+
+ // execute handlers one after another
+ var curHandler = 0;
+ var execHandler = function(req, res){
+ if (handlers[curHandler])
+ handlers[curHandler++](req, res, function(){
+ execHandler(req, res);
+ });
+ }
+
+ // create a non-connect server
+ var server = http.createServer(execHandler).listen(5556, function(){
+ // test it out
+ http.get({
+ host: 'localhost',
+ port: 5556,
+ path: '/'
+ }, function(res){
+ res.setEncoding('utf8');
+ var data = '';
+ res.on('data', function(chunk){
+ data += chunk;
+ });
+ res.on('end', function(){
+ data.should.eql('Ok');
+ server.close();
+ done();
+ });
+ });
+ });
+ })
+
it('should escape the 404 response body', function(done){
var app = connect();
app.request()
Something went wrong with that request. Please try again.