Web
Web is a new node module designed for building web applications. It's intended to replace the built-in http
module as well as middle-ware systems like connect and stack.
As the creator of the connect/stack interface, I've learned a lot over the years about what I could have done better. Also the built-in http
module in node itself has gotten bloated and complex since we've learned as we've gone on. Features can be added a lot easier than they can be removed. As a result, there are many different optional interfaces in the node HTTP interface.
The new interface for web app layers found in web
is closer modeled after the ideas in wsgi, rack, jsgi, strata.js and friends. This allows for easy module composition and a simple but powerful interface through which layers can work together to build a complex web application.
The Old Interface
In the old node interface as exposed by the http
module, you create an HTTP server and pass it an HTTP request handler function.
Usage looks like:
function handler(request, response) {
}
var server = http.createServer(handler);
server.listen(8080);
This interface alone is not easy to wrap application layers and so middle-ware dispatchers like connect and stack were invented to ease the pain. They still have several problems however.
Considering it's history, this module has a pretty good API, but there is only so much you can do if you're learning as you go and are unable to break backwards compatibility. Cruft will accumulate even under the watchful eye of the best maintainers.
The New Way
The new interface is changed slightly.
Firstly, instead of a response
object that's both response stream and object full of random methods, there is a respond
function that accepts status code, headers, and body.
Second, it's not bound to the net
module anymore. I believe that parsing HTTP and listening on a real network socket should be two different responsibilities. If you separate the two, then it's much easier to create mock servers that listen on fake sockets or exotic real servers that aren't TCP based.
var app = function (request, respond) {
}
var handler = require('web').socketHandler(app);
var server = require('net').createServer(handler);
server.listen(8080);