/
index.html
264 lines (235 loc) · 10.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html>
<head>
<title>Connect</title>
<meta http-equiv="content-type" value="text/html; charset=utf-8">
<style type="text/css">
body {
font: 13px "Helvetica Neue", "Lucida Grande", "Arial";
text-align: center;
color: #555;
-webkit-text-stroke: 1px rgba(255, 255, 255, 0.1);
}
h1, h2, h3 {
margin: 0;
font-size: 22px;
font-weight: normal;
color: #343434;
}
h2#Connect {
margin-bottom: 25px;
font-size: 60px;
font-weight: bold;
}
h2#Connect + p {
display: none;
}
h3 {
margin: 35px 0;
padding-left: 10px;
font-size: 16px;
border-left: 15px solid #eee;
}
h2 {
margin-top: 35px;
text-shadow: 1px 2px 2px #ddd;
}
ul {
margin: 10px 35px;
padding: 0;
}
ul li .path {
padding-left: 5px;
font-weight: bold;
}
ul li .line {
padding-right: 5px;
font-style: italic;
}
ul li:first-child .path {
padding-left: 0;
}
p {
line-height: 1.5;
}
p code {
padding: 2px 4px;
border: 1px solid #ddd;
}
p em, li em {
font-weight: bold;
}
pre {
margin: 25px 0 25px 15px;
padding: 15px;
border: 1px solid #ddd;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-webkit-box-shadow: 1px 1px 6px #ddd;
-moz-box-shadow: 1px 1px 6px #ddd;
}
table {
margin-bottom: 35px;
width: 100%;
border-collapse: collapse;
}
table td {
padding: 5px 10px;
font-size: 14px;
}
table tr {
border-bottom: 1px solid #fff;
}
table tr:last-child {
border-bottom: none;
}
table td:first-child {
width: 150px;
color: #343434;
}
#wrapper {
margin: 50px auto;
width: 750px;
text-align: left;
}
#menu {
position: fixed;
top: 15px;
right: 15px;
margin: 0;
padding: 0;
list-style: none;
text-align: right;
}
#menu li.title {
padding: 20px 0 5px 0;
font-size: 12px;
}
code.js { color: #111; }
code.js .comment { color: #999; }
code.js .string { color: #cc0000; }
code.js .number { color: #0000ee; }
code.js .keyword { color: #000; font-weight: bold; }
a {
text-decoration: none;
color: #000;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="wrapper">
<ul id="menu">
<li class="title">API</li>
<li><a href="api.html">extended api docs</a></li>
<li class="title">Middleware</li>
<li><a href="lint.html">lint</a></li>
<li><a href="logger.html">logger</a></li>
<li><a href="router.html">router</a></li>
<li><a href="jsonrpc.html">jsonrpc</a></li>
<li><a href="session.html">session</a></li>
<li><a href="compiler.html">compiler</a></li>
<li><a href="errorHandler.html">errorHandler</a></li>
<li><a href="bodyDecoder.html">bodyDecoder</a></li>
<li><a href="responseTime.html">responseTime</a></li>
<li><a href="cookieDecoder.html">cookieDecoder</a></li>
<li><a href="conditionalGet.html">conditionalGet</a></li>
<li><a href="methodOverride.html">methodOverride</a></li>
<li><a href="staticProvider.html">staticProvider</a></li>
</ul><div class='mp'>
<h2 id="Connect">Connect</h2>
<p class="man-name">
<code>connect</code> - <span class="man-whatis">high performance middleware framework for node</span>
</p>
<p>Connect is a high performance middleware framework for <a href="http://nodejs.org">node</a> featuring
robust middleware for serving static files, advanced routing, cookie and session implementations,
error handling and much more.</p>
<h2 id="Middleware-Usage">Middleware Usage</h2>
<p>Below is an example which shows usage of the <em>logger</em> middleware bundled with Connect, as well as <em>staticProvder</em>.</p>
<pre><code>var connect = require('connect');
var server = connect.createServer(
connect.logger(),
connect.staticProvider(__dirname + '/public')
);
server.listen(3000);
</code></pre>
<h2 id="Middleware-Authoring">Middleware Authoring</h2>
<p>Connect middleware is simply a function which accepts the <em>request</em>, <em>response</em> objects. Optionally
the third parameter <em>next</em> can be used to continue down the middleware stack. For example below is
a middleware layer that simply responds with "hello world".</p>
<pre><code class="js"><span class="keyword">function</span> <span class="variable">helloWorld</span>(<span class="variable">req</span>, <span class="variable">res</span>) {
<span class="variable">res</span>.<span class="variable">writeHead</span>(<span class="number integer">200</span>, { <span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span> });
<span class="variable">res</span>.<span class="variable">end</span>(<span class="string">'hello world'</span>);
}
<span class="variable">connect</span>.<span class="variable">createServer</span>(<span class="variable">helloWorld</span>).<span class="variable">listen</span>(<span class="number integer">3000</span>);
</code></pre>
<p>Lets say we now have some middleware that will require a setup step, this can be done by returning a closure:</p>
<pre><code class="js"><span class="keyword">function</span> <span class="variable">respond</span>(<span class="variable">msg</span>) {
<span class="variable">msg</span> = <span class="variable">msg</span> || <span class="string">'hello world'</span>;
<span class="keyword">return</span> <span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>) {
<span class="variable">res</span>.<span class="variable">writeHead</span>(<span class="number integer">200</span>, { <span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span> });
<span class="variable">res</span>.<span class="variable">end</span>(<span class="variable">msg</span>);
}
}
<span class="variable">connect</span>.<span class="variable">createServer</span>(<span class="variable">respond</span>(<span class="string">'wahoo'</span>)).<span class="variable">listen</span>(<span class="number integer">3000</span>);
</code></pre>
<p>To pass control to the next middleware layer, we may call the <code>next()</code> function with an optional instanceof <code>Error</code>.
Middleware with four parameters is an error handling middleware, the <code>err</code> object can then be logged, used to issue a response, ignored, etc.</p>
<pre><code class="js"><span class="keyword">function</span> <span class="variable">fail</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>) {
<span class="comment">// Exceptions thrown will be automatically passed to next()</span>
<span class="comment">// however for custom exceptions / async exceptions you may pass them</span>
<span class="variable">next</span>(<span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'something broke!'</span>));
}
<span class="keyword">function</span> <span class="variable">errorHandler</span>(<span class="variable">err</span>, <span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>) {
<span class="variable">res</span>.<span class="variable">writeHead</span>(<span class="number integer">500</span>, { <span class="string">'Content-Type'</span>: <span class="string">'text/plain'</span> });
<span class="variable">res</span>.<span class="variable">end</span>(<span class="variable">err</span>.<span class="variable">stack</span>);
}
<span class="variable">connect</span>.<span class="variable">createServer</span>(<span class="variable">fail</span>, <span class="variable">errorHandler</span>).<span class="variable">listen</span>(<span class="number integer">3000</span>);
</code></pre>
<p>To make your middleware available to others, typically you write a module, and export the function itself:</p>
<pre><code class="js"> <span class="comment">// delay.js</span>
<span class="variable">module</span>.<span class="variable">exports</span> = <span class="keyword">function</span>(<span class="variable">ms</span>){
<span class="variable">ms</span> = <span class="variable">ms</span> || <span class="number integer">1000</span>;
<span class="keyword">return</span> <span class="keyword">function</span>(<span class="variable">req</span>, <span class="variable">res</span>, <span class="variable">next</span>){
<span class="variable">setTimeout</span>(<span class="variable">next</span>, <span class="variable">ms</span>);
}
};
<span class="comment">// app.js</span>
<span class="comment">// delay one second before continuing down the stack</span>
<span class="variable">connect</span>.<span class="variable">createServer</span>(<span class="variable">require</span>(<span class="string">'./delay'</span>)(<span class="number integer">1000</span>)).<span class="variable">listen</span>(<span class="number integer">3000</span>);
</code></pre>
<h2 id="Bundled-Middleware">Bundled Middleware</h2>
<p>Connect ships with several helpful middleware modules,
the following are currently provided out of the box:</p>
<pre><code>bodyDecoder Buffers and parses json and urlencoded request bodies (extenable)
conditionalGet Provides 304 "Not Modified" support
errorHandler Handles exceptions thrown, or passed through the stack
gzip Compresses response bodies with gzip executable
lint Aids in middleware development
logger Provides common logger support, and custom log formats
methodOverride Provides faux HTTP method support by using the "_method" key by default
redirect Provides req.redirect() with "magic" urls, ex: req.redirect("back")
compiler Supports arbitrary static compilation of files, currently supports less and sass.
cacheManifest Provides cache manifest for offline apps
jsonrpc Provides JSON-RPC 2.0 support
staticProvider Serves static files
router Provides a feature rich routing API similar to Sinatra and Express
cookieDecoder Provides cookie parsing support
session Provides session support
cache Provides memory caching
repl Read Evaluate Print Loop attached to "/tmp/connect.sock" for inspecting live servers
vhost Virtual host support
</code></pre>
<h3 id="Middleware-Documentation">Middleware Documentation</h3>
<p>To view middleware specific documentation execute:</p>
<pre><code>$ man connect-MIDDLEWARE
</code></pre>
<p>For example:</p>
<pre><code>$ man connect-bodyDecoder
</code></pre>
</div>
</div>
</body>
</html>