.\" generated with Ronn/v0.6.6 .\" http://github.com/rtomayko/ronn/ . .TH "CONNECT" "1" "July 2010" "" "" . .SH "NAME" \fBconnect\fR \- node server runner . .SH "Synopsis" . .nf 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] . .fi . .SH "Description" 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 \fIconnect\fR executable supports launching of both regular \fBnet\.Server\fR, and \fBconnect\.Server\fR instances\. . .P The connect executable supplies \fIinit\.d\fR friendly \fIstart\fR, \fIstop\fR, and \fIrestart\fR commands, and accept a direct path to the module meant to be run, otherwise defaults to trying both \fIapp\.js\fR and \fIserver\.js\fR in the current working directory\. . .P Also to check the status of a process you may use the \fIstatus\fR command, which checks if the process is running\. . .SH "Executable Options" . .nf \-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 . .fi . .SH "Supported Environment Variables" Currently the following environment variables may be set via the \fB\-\-ENV VAL\fR catchall\. For example we can alter the log format used via the command line with `connect \-\-logFormat \":method :uri\"\. . .P Boolean values may use strings such as \fIyes\fR, \fIno\fR, \fItrue\fR, \fIfalse\fR\. . .IP "" 4 . .nf \-\-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 . .fi . .IP "" 0 . .SH "Middleware Usage" Below is an example which shows usage of the \fIlogger\fR middleware bundled with Connect, as well as \fIstaticProvder\fR\. . .IP "" 4 . .nf var connect = require(\'connect\'); module\.exports = connect\.createServer( connect\.logger(), connect\.staticProvider(__dirname + \'/public\') ); . .fi . .IP "" 0 . .P As shown above the module exports a \fBconnect\.Server\fR and does not call the \fBlisten()\fR method directly\. This allows other modules to \"mount\" this app, as well as allowing the \fIconnect\fR executable to control how the server is run\. . .P If you prefer not to use \fIconnect\fR, you can simply create a script executable by \fInode\fR, \fBrequire()\fR the app, then invoke \fBlisten()\fR\. . .IP "" 4 . .nf #!/usr/bin/env node require(\'\./app\')\.listen(); . .fi . .IP "" 0 . .SH "Middleware Authoring" Connect middleware is simply a function which accepts the \fIrequest\fR, \fIresponse\fR objects\. Optionally the third parameter \fInext\fR can be used to continue down the middleware stack\. For example below is a middleware layer that simply responds with \"hello world\"\. . .IP "" 4 . .nf function helloWorld(req, res) { res\.writeHead(200, { \'Content\-Type\': \'text/plain\' }); res\.end(\'hello world\'); } connect\.createServer(helloWorld)\.listen(3000); . .fi . .IP "" 0 . .P Lets say we now have some middleware that will require a setup step, this can be done by returning a closure: . .IP "" 4 . .nf function respond(msg) { msg = msg || \'hello world\'; return function(req, res) { res\.writeHead(200, { \'Content\-Type\': \'text/plain\' }); res\.end(msg); } } connect\.createServer(respond(\'wahoo\'))\.listen(3000); . .fi . .IP "" 0 . .P To pass control to the next middleware layer, we may call the \fBnext()\fR function with an optional instanceof \fBError\fR\. Middleware with four parameters is an error handling middleware, the \fBerr\fR object can then be logged, used to issue a response, ignored, etc\. . .IP "" 4 . .nf function break(req, res, next) { // Exceptions thrown will be automatically passed to next() // however for custom exceptions / async exceptions you may pass them next(new Error(\'something broke!\')); } function errorHandler(err, req, res, next) { res\.writeHead(500, { \'Content\-Type\': \'text/plain\' }); res\.end(err\.stack); } connect\.createServer(break, errorHandler)\.listen(3000); . .fi . .IP "" 0 . .P To make your middleware available to others, typically you write a module, and export the function itself: . .IP "" 4 . .nf // delay\.js module\.exports = function(ms){ ms = ms || 1000; return function(req, res, next){ setTimeout(next, ms); } }; . .fi . .IP "" 0 . .P // app\.js // delay one second before continuing down the stack connect\.createServer(require(\'\./delay\')(1000))\.listen(3000); . .SH "Bundled Middleware" Connect ships with several helpful middleware modules, the following are currently provided out of the box: . .IP "" 4 . .nf 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 . .fi . .IP "" 0 . .SS "Middleware Documentation" To view middleware specific documentation execute: . .IP "" 4 . .nf $ man connect\-MIDDLEWARE . .fi . .IP "" 0 . .P For example: . .IP "" 4 . .nf $ man connect\-bodyDecoder . .fi . .IP "" 0