The accepted answer is really old (and now wrong). Here's the information (with source) based on the current version of Connect (3.0) / Express (4.0).
http / https createServer
which simply takes a callback(req,res) e.g.
var server = http.createServer(function (request, response) {
// respond
response.write('hello client!');
response.end();
});
server.listen(3000);
Middleware is basically any software that sits between your application code and some low level API. Connect extends the built-in HTTP server functionality and adds a plugin framework. The plugins act as middleware and hence connect is a middleware framework
The way it does that is pretty simple (and in fact the code is really short!). As soon as you call var connect = require('connect'); var app = connect();
you get a function app
that can:
.use
(source) to manage plugins (that comes from here because of this simple line of code). Because of 1.) you can do the following :
var app = connect();
// Register with http
http.createServer(app)
.listen(3000);
Combine with 2.) and you get:
var connect = require('connect');
// Create a connect dispatcher
var app = connect()
// register a middleware
.use(function (req, res, next) { next(); });
// Register with http
http.createServer(app)
.listen(3000);
Connect provides a utility function to register itself with http
so that you don't need to make the call to http.createServer(app)
. Its called listen
and the code simply creates a new http server, register's connect as the callback and forwards the arguments to http.listen
. From source
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
So, you can do:
var connect = require('connect');
// Create a connect dispatcher and register with http
var app = connect()
.listen(3000);
console.log('server running on port 3000');
It's still your good old http.createServer
with a plugin framework on top.
ExpressJS and connect are parallel projects. Connect is just a middleware framework, with a nice use
function. Express does not depend on Connect (see package.json). However it does the everything that connect does i.e:
createServer
like connect since it too is just a function that can take a req
/res
pair (source). listen
function to register itself with httpIn addition to what connect provides (which express duplicates), it has a bunch of more features. e.g.
The use
function of ExpressJS and connect is compatible and therefore the middleware is shared. Both are middleware frameworks, express just has more than a simple middleware framework.
My opinion: you are informed enough ^based on above^ to make your own choice.
http.createServer
if you are creating something like connect / expressjs from scratch. http.createServer
Most people should just use ExpressJS.
These might have been true as some point in time, but wrong now:
that inherits an extended version of http.Server
Wrong. It doesn't extend it and as you have seen ... uses it
Express does to Connect what Connect does to the http module
Express 4.0 doesn't even depend on connect. see the current package.json dependencies section