Skip to content
Browse files

Merge pull request #701 from guybedford/master

Allowing connect to be used as middleware
  • Loading branch information...
2 parents 5fb7994 + 7ffb9fa commit 6ed44ac1022e11817d8de15166964fcf06d9bf70 @tj tj committed Jan 13, 2013
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()

0 comments on commit 6ed44ac

Please sign in to comment.
Something went wrong with that request. Please try again.