High performance middleware framework
Connect is an extensible HTTP server framework for node using "plugins" known as middleware.
var connect = require'connect';var http = require'http';var app = connect;// gzip/deflate outgoing responsesvar compression = require'compression';appusecompression;// store session state in browser cookievar cookieSession = require'cookie-session';appusecookieSessionkeys: 'secret1' 'secret2';// parse urlencoded request bodies into req.bodyvar bodyParser = require'body-parser';appusebodyParserurlencoded;// respond to all requestsappuseresend'Hello from Connect!\n';;//create node.js http server and listen on porthttpcreateServerapplisten3000;
Connect is a simple framework to glue together various "middleware" to handle requests.
$ npm install connect
The main component is a Connect "app". This will store all the middleware added and is, itself, a function.
var app = connect;
The core of Connect is "using" middleware. Middleware are added as a "stack"
where incoming requests will execute each middleware one-by-one until a middleware
does not call
next() within it.
appuse// middleware 1next;;appuse// middleware 2next;;
.use() method also takes an optional path string that is matched against
the beginning of the incoming request URL. This allows for basic routing.
appuse'/foo'// req.url starts with "/foo"next;;appuse'/bar'// req.url starts with "/bar"next;;
There are special cases of "error-handling" middleware. There are middleware where the function takes exactly 4 arguments. Errors that occur in the middleware added before the error middleware will invoke this middleware when errors occur.
appuse// an error occurred!;
The last step is to actually use the Connect app in a server. The
is a convenience to start a HTTP server.
var server = applistenport;
The app itself is really just a function with three arguments, so it can also be handed
.createServer() in Node.js.
var server = httpcreateServerapp;
These middleware and libraries are officially supported by the Connect/Express team:
urlencoded. You may also be interested in:
Most of these are exact ports of their Connect 2.x equivalents. The primary exception is
Some middleware previously included with Connect are no longer supported by the Connect/Express team, are replaced by an alternative module, or should be superseded by a better module. Use one of these alternatives instead:
Checkout http-framework for many other compatible middleware!
npm installnpm test
< 1.x- node
< 2.8- node
>= 2.8 < 3- node
>= 3- node