



\href{https://npmjs.org/package/connect}{\texttt{ }} \href{https://npmjs.org/package/connect}{\texttt{ }} \href{https://travis-ci.org/senchalabs/connect}{\texttt{ }} \href{https://coveralls.io/r/senchalabs/connect?branch=master}{\texttt{ }}



Connect is an extensible HTTP server framework for \href{http://nodejs.org}{\texttt{ node}} using \char`\"{}plugins\char`\"{} known as {\itshape middleware}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var connect = require('connect');}
\DoxyCodeLine{var http = require('http');}
\DoxyCodeLine{}
\DoxyCodeLine{var app = connect();}
\DoxyCodeLine{}
\DoxyCodeLine{// gzip/deflate outgoing responses}
\DoxyCodeLine{var compression = require('compression');}
\DoxyCodeLine{app.use(compression());}
\DoxyCodeLine{}
\DoxyCodeLine{// store session state in browser cookie}
\DoxyCodeLine{var cookieSession = require('cookie-\/session');}
\DoxyCodeLine{app.use(cookieSession(\{}
\DoxyCodeLine{    keys: ['secret1', 'secret2']}
\DoxyCodeLine{\}));}
\DoxyCodeLine{}
\DoxyCodeLine{// parse urlencoded request bodies into req.body}
\DoxyCodeLine{var bodyParser = require('body-\/parser');}
\DoxyCodeLine{app.use(bodyParser.urlencoded(\{extended: false\}));}
\DoxyCodeLine{}
\DoxyCodeLine{// respond to all requests}
\DoxyCodeLine{app.use(function(req, res)\{}
\DoxyCodeLine{  res.end('Hello from Connect!\(\backslash\)n');}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{//create node.js http server and listen on port}
\DoxyCodeLine{http.createServer(app).listen(3000); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6435}{}\doxysubsection{Getting Started}\label{md_sfcode_node_modules_connect_README_autotoc_md6435}
Connect is a simple framework to glue together various \char`\"{}middleware\char`\"{} to handle requests.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6436}{}\doxysubsubsection{Install Connect}\label{md_sfcode_node_modules_connect_README_autotoc_md6436}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install connect }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6437}{}\doxysubsubsection{Create an app}\label{md_sfcode_node_modules_connect_README_autotoc_md6437}
The main component is a Connect \char`\"{}app\char`\"{}. This will store all the middleware added and is, itself, a function.


\begin{DoxyCode}{0}
\DoxyCodeLine{var app = connect(); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6438}{}\doxysubsubsection{Use middleware}\label{md_sfcode_node_modules_connect_README_autotoc_md6438}
The core of Connect is \char`\"{}using\char`\"{} middleware. Middleware are added as a \char`\"{}stack\char`\"{} where incoming requests will execute each middleware one-\/by-\/one until a middleware does not call {\ttfamily next()} within it.


\begin{DoxyCode}{0}
\DoxyCodeLine{app.use(function middleware1(req, res, next) \{}
\DoxyCodeLine{  // middleware 1}
\DoxyCodeLine{  next();}
\DoxyCodeLine{\});}
\DoxyCodeLine{app.use(function middleware2(req, res, next) \{}
\DoxyCodeLine{  // middleware 2}
\DoxyCodeLine{  next();}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6439}{}\doxysubsubsection{Mount middleware}\label{md_sfcode_node_modules_connect_README_autotoc_md6439}
The {\ttfamily .use()} method also takes an optional path string that is matched against the beginning of the incoming request URL. This allows for basic routing.


\begin{DoxyCode}{0}
\DoxyCodeLine{app.use('/foo', function fooMiddleware(req, res, next) \{}
\DoxyCodeLine{  // req.url starts with "{}/foo"{}}
\DoxyCodeLine{  next();}
\DoxyCodeLine{\});}
\DoxyCodeLine{app.use('/bar', function barMiddleware(req, res, next) \{}
\DoxyCodeLine{  // req.url starts with "{}/bar"{}}
\DoxyCodeLine{  next();}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6440}{}\doxysubsubsection{Error middleware}\label{md_sfcode_node_modules_connect_README_autotoc_md6440}
There are special cases of \char`\"{}error-\/handling\char`\"{} middleware. There are middleware where the function takes exactly 4 arguments. When a middleware passes an error to {\ttfamily next}, the app will proceed to look for the error middleware that was declared after that middleware and invoke it, skipping any error middleware above that middleware and any non-\/error middleware below.


\begin{DoxyCode}{0}
\DoxyCodeLine{// regular middleware}
\DoxyCodeLine{app.use(function (req, res, next) \{}
\DoxyCodeLine{  // i had an error}
\DoxyCodeLine{  next(new Error('boom!'));}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{// error middleware for errors that occurred in middleware}
\DoxyCodeLine{// declared before this}
\DoxyCodeLine{app.use(function onerror(err, req, res, next) \{}
\DoxyCodeLine{  // an error occurred!}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6441}{}\doxysubsubsection{Create a server from the app}\label{md_sfcode_node_modules_connect_README_autotoc_md6441}
The last step is to actually use the Connect app in a server. The {\ttfamily .listen()} method is a convenience to start a HTTP server (and is identical to the {\ttfamily http.\+Server}\textquotesingle{}s {\ttfamily listen} method in the version of Node.\+js you are running).


\begin{DoxyCode}{0}
\DoxyCodeLine{var server = app.listen(port); }

\end{DoxyCode}


The app itself is really just a function with three arguments, so it can also be handed to {\ttfamily .create\+Server()} in Node.\+js.


\begin{DoxyCode}{0}
\DoxyCodeLine{var server = http.createServer(app); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6442}{}\doxysubsection{Middleware}\label{md_sfcode_node_modules_connect_README_autotoc_md6442}
These middleware and libraries are officially supported by the Connect/\+Express team\+:


\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/body-parser}{\texttt{ body-\/parser}} -\/ previous {\ttfamily body\+Parser}, {\ttfamily json}, and {\ttfamily urlencoded}. You may also be interested in\+:
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/body}{\texttt{ body}}
\item \href{https://www.npmjs.com/package/co-body}{\texttt{ co-\/body}}
\item \href{https://www.npmjs.com/package/raw-body}{\texttt{ raw-\/body}}
\end{DoxyItemize}
\item \href{https://www.npmjs.com/package/compression}{\texttt{ compression}} -\/ previously {\ttfamily compress}
\item \href{https://www.npmjs.com/package/connect-timeout}{\texttt{ connect-\/timeout}} -\/ previously {\ttfamily timeout}
\item \href{https://www.npmjs.com/package/cookie-parser}{\texttt{ cookie-\/parser}} -\/ previously {\ttfamily cookie\+Parser}
\item \href{https://www.npmjs.com/package/cookie-session}{\texttt{ cookie-\/session}} -\/ previously {\ttfamily cookie\+Session}
\item \href{https://www.npmjs.com/package/csurf}{\texttt{ csurf}} -\/ previously {\ttfamily csrf}
\item \href{https://www.npmjs.com/package/errorhandler}{\texttt{ errorhandler}} -\/ previously {\ttfamily error-\/handler}
\item \href{https://www.npmjs.com/package/express-session}{\texttt{ express-\/session}} -\/ previously {\ttfamily session}
\item \href{https://www.npmjs.com/package/method-override}{\texttt{ method-\/override}} -\/ previously {\ttfamily method-\/override}
\item \href{https://www.npmjs.com/package/morgan}{\texttt{ morgan}} -\/ previously {\ttfamily logger}
\item \href{https://www.npmjs.com/package/response-time}{\texttt{ response-\/time}} -\/ previously {\ttfamily response-\/time}
\item \href{https://www.npmjs.com/package/serve-favicon}{\texttt{ serve-\/favicon}} -\/ previously {\ttfamily favicon}
\item \href{https://www.npmjs.com/package/serve-index}{\texttt{ serve-\/index}} -\/ previously {\ttfamily directory}
\item \href{https://www.npmjs.com/package/serve-static}{\texttt{ serve-\/static}} -\/ previously {\ttfamily static}
\item \href{https://www.npmjs.com/package/vhost}{\texttt{ vhost}} -\/ previously {\ttfamily vhost}
\end{DoxyItemize}

Most of these are exact ports of their Connect 2.\+x equivalents. The primary exception is {\ttfamily cookie-\/session}.

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\+:


\begin{DoxyItemize}
\item {\ttfamily cookie\+Parser}
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/cookies}{\texttt{ cookies}} and \href{https://www.npmjs.com/package/keygrip}{\texttt{ keygrip}}
\end{DoxyItemize}
\item {\ttfamily limit}
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/raw-body}{\texttt{ raw-\/body}}
\end{DoxyItemize}
\item {\ttfamily multipart}
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/connect-multiparty}{\texttt{ connect-\/multiparty}}
\item \href{https://www.npmjs.com/package/connect-busboy}{\texttt{ connect-\/busboy}}
\end{DoxyItemize}
\item {\ttfamily query}
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/qs}{\texttt{ qs}}
\end{DoxyItemize}
\item {\ttfamily static\+Cache}
\begin{DoxyItemize}
\item \href{https://www.npmjs.com/package/st}{\texttt{ st}}
\item \href{https://www.npmjs.com/package/connect-static}{\texttt{ connect-\/static}}
\end{DoxyItemize}
\end{DoxyItemize}

Checkout \href{https://github.com/Raynos/http-framework/wiki/Modules}{\texttt{ http-\/framework}} for many other compatible middleware!\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6443}{}\doxysubsection{API}\label{md_sfcode_node_modules_connect_README_autotoc_md6443}
The Connect API is very minimalist, enough to create an app and add a chain of middleware.

When the {\ttfamily connect} module is required, a function is returned that will construct a new app when called.


\begin{DoxyCode}{0}
\DoxyCodeLine{// require module}
\DoxyCodeLine{var connect = require('connect')}
\DoxyCodeLine{}
\DoxyCodeLine{// create app}
\DoxyCodeLine{var app = connect() }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6444}{}\doxysubsubsection{app(req, res\mbox{[}, next\mbox{]})}\label{md_sfcode_node_modules_connect_README_autotoc_md6444}
The {\ttfamily app} itself is a function. This is just an alias to {\ttfamily app.\+handle}.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6445}{}\doxysubsubsection{app.\+handle(req, res\mbox{[}, out\mbox{]})}\label{md_sfcode_node_modules_connect_README_autotoc_md6445}
Calling the function will run the middleware stack against the given Node.\+js http request ({\ttfamily req}) and response ({\ttfamily res}) objects. An optional function {\ttfamily out} can be provided that will be called if the request (or error) was not handled by the middleware stack.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6446}{}\doxysubsubsection{app.\+listen(\mbox{[}...\mbox{]})}\label{md_sfcode_node_modules_connect_README_autotoc_md6446}
Start the app listening for requests. This method will internally create a Node.\+js HTTP server and call {\ttfamily .listen()} on it.

This is an alias to the {\ttfamily server.\+listen()} method in the version of Node.\+js running, so consult the Node.\+js documentation for all the different variations. The most common signature is \href{https://nodejs.org/dist/latest-v6.x/docs/api/http.html\#http_server_listen_port_hostname_backlog_callback}{\texttt{ {\ttfamily app.\+listen(port)}}}.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6447}{}\doxysubsubsection{app.\+use(fn)}\label{md_sfcode_node_modules_connect_README_autotoc_md6447}
Use a function on the app, where the function represents a middleware. The function will be invoked for every request in the order that {\ttfamily app.\+use} is called. The function is called with three arguments\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{app.use(function (req, res, next) \{}
\DoxyCodeLine{  // req is the Node.js http request object}
\DoxyCodeLine{  // res is the Node.js http response object}
\DoxyCodeLine{  // next is a function to call to invoke the next middleware}
\DoxyCodeLine{\}) }

\end{DoxyCode}


In addition to a plan function, the {\ttfamily fn} argument can also be a Node.\+js HTTP server instance or another Connect app instance.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6448}{}\doxysubsubsection{app.\+use(route, fn)}\label{md_sfcode_node_modules_connect_README_autotoc_md6448}
Use a function on the app, where the function represents a middleware. The function will be invoked for every request in which the URL ({\ttfamily req.\+url} property) starts with the given {\ttfamily route} string in the order that {\ttfamily app.\+use} is called. The function is called with three arguments\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{app.use('/foo', function (req, res, next) \{}
\DoxyCodeLine{  // req is the Node.js http request object}
\DoxyCodeLine{  // res is the Node.js http response object}
\DoxyCodeLine{  // next is a function to call to invoke the next middleware}
\DoxyCodeLine{\}) }

\end{DoxyCode}


In addition to a plan function, the {\ttfamily fn} argument can also be a Node.\+js HTTP server instance or another Connect app instance.

The {\ttfamily route} is always terminated at a path separator ({\ttfamily /}) or a dot ({\ttfamily .}) character. This means the given routes {\ttfamily /foo/} and {\ttfamily /foo} are the same and both will match requests with the URLs {\ttfamily /foo}, {\ttfamily /foo/}, {\ttfamily /foo/bar}, and {\ttfamily /foo.bar}, but not match a request with the URL {\ttfamily /foobar}.

The {\ttfamily route} is matched in a case-\/insensitive manor.

In order to make middleware easier to write to be agnostic of the {\ttfamily route}, when the {\ttfamily fn} is invoked, the {\ttfamily req.\+url} will be altered to remove the {\ttfamily route} part (and the original will be available as {\ttfamily req.\+original\+Url}). For example, if {\ttfamily fn} is used at the route {\ttfamily /foo}, the request for {\ttfamily /foo/bar} will invoke {\ttfamily fn} with `req.\+url === '/bar\textquotesingle{}{\ttfamily  and}req.\+original\+Url === \textquotesingle{}/foo/bar\textquotesingle{}\`{}.\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6449}{}\doxysubsection{Running Tests}\label{md_sfcode_node_modules_connect_README_autotoc_md6449}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install}
\DoxyCodeLine{npm test }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6450}{}\doxysubsection{People}\label{md_sfcode_node_modules_connect_README_autotoc_md6450}
The Connect project would not be the same without all the people involved.

The original author of Connect is \href{https://github.com/tj}{\texttt{ TJ Holowaychuk}}

The current lead maintainer is \href{https://github.com/dougwilson}{\texttt{ Douglas Christopher Wilson}}

\href{https://github.com/senchalabs/connect/graphs/contributors}{\texttt{ List of all contributors}}\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6451}{}\doxysubsection{Node Compatibility}\label{md_sfcode_node_modules_connect_README_autotoc_md6451}

\begin{DoxyItemize}
\item Connect {\ttfamily $<$ 1.\+x} -\/ node {\ttfamily 0.\+2}
\item Connect {\ttfamily 1.\+x} -\/ node {\ttfamily 0.\+4}
\item Connect {\ttfamily $<$ 2.\+8} -\/ node {\ttfamily 0.\+6}
\item Connect {\ttfamily $>$= 2.\+8 $<$ 3} -\/ node {\ttfamily 0.\+8}
\item Connect {\ttfamily $>$= 3} -\/ node {\ttfamily 0.\+10}, {\ttfamily 0.\+12}, {\ttfamily 4.\+x}, {\ttfamily 5.\+x}, {\ttfamily 6.\+x}, {\ttfamily 7.\+x}, {\ttfamily 8.\+x}, {\ttfamily 9.\+x}, {\ttfamily 10.\+x}, {\ttfamily 11.\+x}, {\ttfamily 12.\+x}; io.\+js {\ttfamily 1.\+x}, {\ttfamily 2.\+x}, {\ttfamily 3.\+x}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_connect_README_autotoc_md6452}{}\doxysubsection{License}\label{md_sfcode_node_modules_connect_README_autotoc_md6452}
\mbox{[}MIT\mbox{]}(LICENSE) 