connect-form
Version:
urlencoded / multipart form parsing middleware for Connect
333 lines (290 loc) • 14.5 kB
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="flash.html">flash</a></li>
<li><a href="logger.html">logger</a></li>
<li><a href="format.html">format</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="redirect.html">redirect</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><code>connect</code> - node server runner</p>
<h2 id="Synopsis">Synopsis</h2>
<pre><code>connect [-H|--host ADDR] [-p|--port NUM]
[-n|--workers NUM] [-I|--include PATH]
[-E|--env ENV] [-e|--eval CODE] [-C|--chdir PATH]
[-c|--config PATH] [-P|--pidfile PATH]
[-l|--logfile PATH] [-u|--user ID|USER] [-g|--group ID|GROUP]
[-v|--verbose] [-V|--version] [-K|--no-color]
[-h|--help] [--ENV VAL]
start|stop|restart|status [PATH]
</code></pre>
<h2 id="Description">Description</h2>
<p>Connect is a dual purpose library, aiding in both rapid development, and deployment of node servers. Connect "middleware" can be stacked to create a robust application within minutes. The <em>connect</em> executable supports launching of both regular <code>net.Server</code>, and <code>connect.Server</code> instances.</p>
<p>The connect executable supplies <em>init.d</em> friendly <em>start</em>, <em>stop</em>, and <em>restart</em> commands, and accept a direct path to the module meant to be run, otherwise defaults to trying both <em>app.js</em> and <em>server.js</em> in the current working directory.</p>
<p>Also to check the status of a process you may use the <em>status</em> command, which
checks if the process is running.</p>
<h2 id="Executable-Options">Executable Options</h2>
<pre><code>-H, --host ADDR Host address, defaults to INADDR_ANY
-p, --port NUM Port number, defaults to 3000
-n, --workers NUM Number of worker processes to spawn
-I, --include PATH Unshift the given path to require.paths
-E, --env ENV Set environment, defaults to "development"
-e, --eval CODE Evaluate the given string
-C, --chdir PATH Change to the given path
-c, --config PATH Load configuration module
-P, --pidfile PATH PID file, defaults to pids/connect.pid
-l, --logfile PATH Log file, defaults to logs/connect.log
-u, --user ID|USER Change user with setuid()
-g, --group ID|GROUP Change group with setgid()
-v, --verbose Display verbose output
-V, --version Output connect version
-K, --no-color Suppress colored terminal output
-h, --help Display help information
--ENV VAL Sets the given connect environment variable
</code></pre>
<h2 id="Supported-Environment-Variables">Supported Environment Variables</h2>
<p>Currently the following environment variables may be set
via the <code>--ENV VAL</code> catchall. For example we can alter the log
format used via the command line with `connect --logFormat ":method :uri".</p>
<p>Boolean values may use strings such as <em>yes</em>, <em>no</em>, <em>true</em>, <em>false</em>.</p>
<pre><code>--logFormat STR Custom log format
--dumpExceptions BOOL Dump exceptions to stderr
--showErrorMessage BOOL Show exception message in response (recommended for development only)
--showErrorStack BOOL Show exception stack trace (recommended for development only)
--methodOverrideKey STR Override the default method key of "_method"
--compilerSrc PATH Compiler source root directory
--compilerDest PATH Compiler destination directory
</code></pre>
<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');
module.exports = connect.createServer(
connect.logger(),
connect.staticProvider(__dirname + '/public')
);
</code></pre>
<p>As shown above the module exports a <code>connect.Server</code> and does not call the <code>listen()</code> method directly. This allows other modules to "mount" this app, as well as allowing the <em>connect</em> executable to control how the server is run.</p>
<p>If you prefer not to use <em>connect</em>, you can simply create a script executable by <em>node</em>, <code>require()</code> the app, then invoke <code>listen()</code>.</p>
<pre><code>#!/usr/bin/env node
require('./app').listen();
</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="keyword">break</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="keyword">break</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>);
}
};
</code></pre>
<p> // app.js
// delay one second before continuing down the stack
connect.createServer(require('./delay')(1000)).listen(3000);</p>
<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
debug Outputs debugging console to all html responses
format Handles url path extensions or "formats"
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
responseTime Responds with the X-Response-Time header in milliseconds
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
pubsub Publish subscribe messaging support
jsonrpc JSON-RPC 2.0 support
format Populates req.format for urls such as "/products.json"
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>