Skip to content
Myr

guide.1

This file

Path

docs/guide.1

1,132 lines · 22.5 KiB

Last changed by

Added "Apps using express" section, PM me to add to the listTj Holowaychukbefore adoption7328842

History of this file

Take it

1.\" generated with Ronn/v0.6.62.\" http://github.com/rtomayko/ronn/3.4.TH "GUIDE" "" "July 2010" "" ""5.6.SS "Installation"7curl:8.9.IP "" 410.11.nf1213$ curl \-# http://expressjs\.com/install\.sh | sh14.15.fi16.17.IP "" 018.19.P20npm:21.22.IP "" 423.24.nf2526$ npm install express27.28.fi29.30.IP "" 031.32.P33git clone, first update the submodules:34.35.IP "" 436.37.nf3839$ git submodule update \-\-init40$ make install41$ make install\-support42.43.fi44.45.IP "" 046.47.SS "Creating An Application"48The \fIexpress\.Server\fR now inherits from \fIhttp\.Server\fR, however follows the same idiom by providing \fIexpress\.createServer()\fR as shown below\. This means that you can utilize Express server\'s transparently with other libraries\.49.50.IP "" 451.52.nf5354var app = require(\'express\')\.createServer();5556app\.get(\'/\', function(req, res){57    res\.send(\'hello world\');58});5960app\.listen(3000);61.62.fi63.64.IP "" 065.66.SS "Configuration"67Express supports arbitrary environments, such as \fIproduction\fR and \fIdevelopment\fR\. Developers can use the \fIconfigure()\fR method to setup needs required by the current environment\. When \fIconfigure()\fR is called without an environment name it will be run in \fIevery\fR environment prior to the environment specific callback\.68.69.P70In the example below we only \fIdumpExceptions\fR, and respond with exception stack traces in \fIdevelopment\fR mode, however for both environments we utilize \fImethodOverride\fR and \fIbodyDecoder\fR\. Note the use of \fIapp\.router\fR, which can (optionally) be used to mount the application routes, otherwise the first call to \fIapp\.{get,put,del,post}()\fR will mount the routes\.71.72.IP "" 473.74.nf7576app\.configure(function(){77    app\.use(connect\.methodOverride());78    app\.use(connect\.bodyDecoder());79    app\.use(app\.router);80    app\.use(connect\.staticProvider(__dirname + \'/public\'));81});8283app\.configure(\'development\', function(){84    app\.use(connect\.errorHandler({ dumpExceptions: true, showStack: true }));85});8687app\.configure(\'production\', function(){88    app\.use(connect\.errorHandler());89});90.91.fi92.93.IP "" 094.95.P96For internal and arbitrary settings Express provides the \fIset(key[, val])\fR, \fIenable(key)\fR, \fIdisable(key)\fR methods:97.98.IP "" 499.100.nf101102app\.configure(function(){103    app\.set(\'views\', __dirname + \'/views\');104    app\.set(\'views\');105    // => \"\.\.\. views directory \.\.\.\"106107    app\.enable(\'some feature\');108    // same as app\.set(\'some feature\', true);109110    app\.disable(\'some feature\');111    // same as app\.set(\'some feature\', false);112});113.114.fi115.116.IP "" 0117.118.P119To alter the environment we can set the \fICONNECT_ENV\fR environment variable, or more specifically \fIEXPRESS_ENV\fR, for example:120.121.IP "" 4122.123.nf124125$ EXPRESS_ENV=production node app\.js126.127.fi128.129.IP "" 0130.131.SS "Settings"132Express supports the following settings out of the box:133.134.IP "\(bu" 4135\fIenv\fR Application environment set internally, use \fIapp\.set(\'env\')\fR on \fIServer#listen()\fR136.137.IP "\(bu" 4138\fIhome\fR Application base path used for \fIres\.redirect()\fR and transparently handling mounted apps\.139.140.IP "\(bu" 4141\fIviews\fR Root views directory defaulting to \fBCWD/views\fR142.143.IP "\(bu" 4144\fIview engine\fR Default view engine name for views rendered without extensions145.146.IP "\(bu" 4147\fIreload views\fR Reloads altered views, by default watches for \fImtime\fR changes with with a 500 millisecond interval\.148.149.IP "" 0150.151.SS "Routing"152Express utilizes the HTTP verbs to provide a meaningful, expressive routing API\. For example we may want to render a user\'s account for the path \fI/user/12\fR, this can be done by defining the route below\. The values associated to the named placeholders are available as \fBreq\.params\fR\.153.154.IP "" 4155.156.nf157158app\.get(\'/user/:id\', function(req, res){159    res\.send(\'user \' + req\.params\.id);160});161.162.fi163.164.IP "" 0165.166.P167A route is simple a string which is compiled to a \fIRegExp\fR internally\. For example when \fI/user/:id\fR is compiled, a simplified version of the regexp may look similar to:168.169.IP "" 4170.171.nf172173\\/user\\/([^\\/]+)\\/?174.175.fi176.177.IP "" 0178.179.P180Regular expression literals may also be passed for complex uses\. Since capture groups with literal \fIRegExp\fR\'s are anonymous we can access them directly \fBreq\.params\fR\.181.182.IP "" 4183.184.nf185186app\.get(/^\\/users?(?:\\/(\\d+)(?:\\\.\\\.(\\d+))?)?/, function(req, res){187    res\.send(req\.params);188});189.190.fi191.192.IP "" 0193.194.P195Curl requests against the previously defined route:196.197.IP "" 4198.199.nf200201   $ curl http://dev:3000/user202   [null,null]203   $ curl http://dev:3000/users204   [null,null]205   $ curl http://dev:3000/users/1206   [\"1\",null]207   $ curl http://dev:3000/users/1\.\.15208   [\"1\",\"15\"]209.210.fi211.212.IP "" 0213.214.P215Below are some route examples, and the associated paths that they may consume:216.217.IP "" 4218.219.nf220221 \"/user/:id\"222 /user/12223224 \"/users/:id?\"225 /users/5226 /users227228 \"/files/*\"229 /files/jquery\.js230 /files/javascripts/jquery\.js231232 \"/file/*\.*\"233 /files/jquery\.js234 /files/javascripts/jquery\.js235236 \"/user/:id/:operation?\"237 /user/1238 /user/1/edit239240 \"/products\.:format\"241 /products\.json242 /products\.xml243244 \"/products\.:format?\"245 /products\.json246 /products\.xml247 /products248.249.fi250.251.IP "" 0252.253.SS "Passing Route Control"254We may pass control to the next \fImatching\fR route, by calling the \fIthird\fR argument, the \fInext()\fR function\. When a match cannot be made, control is passed back to Connect, and middleware continue to be invoked\.255.256.IP "" 4257.258.nf259260app\.get(\'/users/:id?\', function(req, res){261    var id = req\.params\.id;262    if (id) {263        // do something264    } else {265        next();266    }267});268269app\.get(\'/users\', function(req, res){270    // do something else271});272.273.fi274.275.IP "" 0276.277.SS "Middleware"278The Express \fIPlugin\fR is no more! middleware via Connect \fIhttp://github\.com/extjs/Connect\fR can be passed to \fIexpress\.createServer()\fR as you would with a regular Connect server\. For example:279.280.IP "" 4281.282.nf283284var connect = require(\'connect\'),285    express = require(\'express\');286287var app = express\.createServer(288    connect\.logger(),289    connect\.bodyDecoder()290);291.292.fi293.294.IP "" 0295.296.P297Alternatively we can \fIuse()\fR them which is useful when adding middleware within \fIconfigure()\fR blocks:298.299.IP "" 4300.301.nf302303app\.use(connect\.logger({ format: \':method :uri\' }));304.305.fi306.307.IP "" 0308.309.SS "Error Handling"310Express provides the \fIapp\.error()\fR method which receives exceptions thrown within a route, or passed to \fInext(err)\fR\. Below is an example which serves different pages based on our ad\-hoc \fINotFound\fR exception:311.312.IP "" 4313.314.nf315316function NotFound(msg){317    this\.name = \'NotFound\';318    Error\.call(this, msg);319    Error\.captureStackTrace(this, arguments\.callee);320}321322sys\.inherits(NotFound, Error);323324app\.get(\'/404\', function(req, res){325    throw new NotFound;326});327328app\.get(\'/500\', function(req, res){329    throw new Error(\'keyboard cat!\');330});331.332.fi333.334.IP "" 0335.336.P337We can call \fIapp\.error()\fR several times as shown below\. Here we check for an instanceof \fINotFound\fR and show the 404 page, or we pass on to the next error handler\.338.339.IP "" 4340.341.nf342343app\.error(function(err, req, res, next){344    if (err instanceof NotFound) {345        res\.render(\'404\.jade\');346    } else {347        next(err);348    }349});350.351.fi352.353.IP "" 0354.355.P356Here we assume all errors as 500 for the simplicity of this demo, however you can choose whatever you like357.358.IP "" 4359.360.nf361362app\.error(function(err, req, res){363    res\.render(\'500\.jade\', {364       locals: {365           error: err366       }367    });368});369.370.fi371.372.IP "" 0373.374.P375Our apps could also utilize the Connect \fIerrorHandler\fR middleware to report on exceptions\. For example if we wish to output exceptions in \"development\" mode to \fIstderr\fR we can use:376.377.IP "" 4378.379.nf380381app\.use(connect\.errorHandler({ dumpExceptions: true }));382.383.fi384.385.IP "" 0386.387.P388Also during development we may want fancy html pages to show exceptions that are passed or thrown, so we can set \fIshowStack\fR to true:389.390.IP "" 4391.392.nf393394app\.use(connect\.errorHandler({ showStack: true, dumpExceptions: true }));395.396.fi397.398.IP "" 0399.400.P401The \fIerrorHandler\fR middleware also responds with \fIjson\fR if \fIAccept: application/json\fR is present, which is useful for developing apps that rely heavily on client\-side JavaScript\.402.403.SS "View Rendering"404View filenames take the form \fINAME\fR\.\fIENGINE\fR, where \fIENGINE\fR is the name of the module that will be required\. For example the view \fIlayout\.ejs\fR will tell the view system to \fIrequire(\'ejs\')\fR, the module being loaded must (currently) export the method \fIexports\.render(str, options)\fR to comply with Express, however with will likely be extensible in the future\.405.406.P407Below is an example using Haml\.js \fIhttp://github\.com/visionmedia/haml\.js\fR to render \fIindex\.html\fR, and since we do not use \fIlayout: false\fR the rendered contents of \fIindex\.html\fR will be passed as the \fIbody\fR local variable in \fIlayout\.haml\fR\.408.409.IP "" 4410.411.nf412413app\.get(\'/\', function(req, res){414    res\.render(\'index\.haml\', {415        locals: { title: \'My Site\' }416    });417});418.419.fi420.421.IP "" 0422.423.P424The new \fIview engine\fR setting allows us to specify our default template engine, so for example when using Jade \fIhttp://github\.com/visionmedia/jade\fR we could set:425.426.IP "" 4427.428.nf429430app\.set(\'view engine\', \'jade\');431.432.fi433.434.IP "" 0435.436.P437Allowing us to render with:438.439.IP "" 4440.441.nf442443res\.render(\'index\');444.445.fi446.447.IP "" 0448.449.P450vs:451.452.IP "" 4453.454.nf455456res\.render(\'index\.jade\');457.458.fi459.460.IP "" 0461.462.P463When \fIview engine\fR is set, extensions are entirely optional, however we can still mix and match template engines:464.465.IP "" 4466.467.nf468469res\.render(\'another\-page\.ejs\');470.471.fi472.473.IP "" 0474.475.SS "View Partials"476The Express view system has built\-in support for partials and collections, which are sort of \"mini\" views representing a document fragment\. For example rather than iterating in a view to display comments, we would use a partial with collection support:477.478.IP "" 4479.480.nf481482partial(\'comment\.haml\', { collection: comments });483.484.fi485.486.IP "" 0487.488.P489To make things even less verbose we can assume the extension as \fI\.haml\fR when omitted, however if we wished we could use an ejs partial, within a haml view for example\.490.491.IP "" 4492.493.nf494495partial(\'comment\', { collection: comments });496.497.fi498.499.IP "" 0500.501.P502And once again even further, when rendering a collection we can simply pass an array, if no other options are desired:503.504.IP "" 4505.506.nf507508partial(\'comments\', comments);509.510.fi511.512.IP "" 0513.514.P515When using the partial collection support a few \"magic\" variables are provided for free:516.517.IP "\(bu" 4518\fIfirstInCollection\fR True if this is the first object519.520.IP "\(bu" 4521\fIindexInCollection\fR Index of the object in the collection522.523.IP "\(bu" 4524_lastInCollection _ True if this is the last object525.526.IP "" 0527.528.SS "Template Engines"529Below are a few template engines commonly used with Express:530.531.IP "\(bu" 4532Jade \fIhttp://github\.com/visionmedia/jade\fR haml\.js successor533.534.IP "\(bu" 4535Haml \fIhttp://github\.com/visionmedia/haml\.js\fR indented templates536.537.IP "\(bu" 4538EJS \fIhttp://github\.com/visionmedia/ejs\fR Embedded JavaScript539.540.IP "" 0541.542.SS "req\.header(key[, defaultValue])"543Get the case\-insensitive request header \fIkey\fR, with optional \fIdefaultValue\fR:544.545.IP "" 4546.547.nf548549req\.header(\'Host\');550req\.header(\'host\');551req\.header(\'Accept\', \'*/*\');552.553.fi554.555.IP "" 0556.557.SS "req\.accepts(type)"558Check if the \fIAccept\fR header is present, and includes the given \fItype\fR\.559.560.P561When the \fIAccept\fR header is not present \fItrue\fR is returned\. Otherwise the given \fItype\fR is matched by an exact match, and then subtypes\. You may pass the subtype such as \"html\" which is then converted internally to \"text/html\" using the mime lookup table\.562.563.IP "" 4564.565.nf566567// Accept: text/html568req\.accepts(\'html\');569// => true570571// Accept: text/*; application/json572req\.accepts(\'html\');573req\.accepts(\'text/html\');574req\.accepts(\'text/plain\');575req\.accepts(\'application/json\');576// => true577578req\.accepts(\'image/png\');579req\.accepts(\'png\');580// => false581.582.fi583.584.IP "" 0585.586.SS "req\.param(name)"587Return the value of param \fIname\fR when present\.588.589.IP "\(bu" 4590Checks route placeholders (\fIreq\.params\fR), ex: /user/:id591.592.IP "\(bu" 4593Checks query string params (\fIreq\.query\fR), ex: ?id=12594.595.IP "\(bu" 4596Checks urlencoded body params (\fIreq\.body\fR), ex: id=12597.598.IP "" 0599.600.P601To utilize urlencoded request bodies, \fIreq\.body\fR should be an object\. This can be done by using the \fIconnect\.bodyDecoder\fR middleware\.602.603.SS "req\.flash(type[, msg])"604Queue flash \fImsg\fR of the given \fItype\fR\.605.606.IP "" 4607.608.nf609610req\.flash(\'info\', \'email sent\');611req\.flash(\'error\', \'email delivery failed\');612req\.flash(\'info\', \'email re\-sent\');613// => 2614615req\.flash(\'info\');616// => [\'email sent\', \'email re\-sent\']617618req\.flash(\'info\');619// => []620621req\.flash();622// => { error: [\'email delivery failed\'], info: [] }623.624.fi625.626.IP "" 0627.628.SS "req\.isXMLHttpRequest"629Also aliased as \fIreq\.xhr\fR, this getter checks the \fIX\-Requested\-With\fR header to see if it was issued by an \fIXMLHttpRequest\fR:630.631.IP "" 4632.633.nf634635req\.xhr636req\.isXMLHttpRequest637.638.fi639.640.IP "" 0641.642.SS "res\.header(key[, val])"643Get or set the response header \fIkey\fR\.644.645.IP "" 4646.647.nf648649res\.header(\'Content\-Length\');650// => undefined651652res\.header(\'Content\-Length\', 123);653// => 123654655res\.header(\'Content\-Length\');656// => 123657.658.fi659.660.IP "" 0661.662.SS "res\.contentType(type)"663Sets the \fIContent\-Type\fR response header to the given \fItype\fR\.664.665.IP "" 4666.667.nf668669  var filename = \'path/to/image\.png\';670  res\.contentType(filename);671  // res\.headers[\'Content\-Type\'] is now \"image/png\"672.673.fi674.675.IP "" 0676.677.SS "res\.attachment([filename])"678Sets the \fIContent\-Disposition\fR response header to \"attachment\", with optional \fIfilename\fR\.679.680.IP "" 4681.682.nf683684  res\.attachment(\'path/to/my/image\.png\');685.686.fi687.688.IP "" 0689.690.SS "res\.sendfile(path)"691Used by \fBres\.download()\fR to transfer an arbitrary file\.692.693.IP "" 4694.695.nf696697res\.sendfile(\'path/to/my\.file\');698.699.fi700.701.IP "" 0702.703.P704This is \fInot\fR a substitution for Connect\'s \fIstaticProvider\fR middleware, it does not support HTTP caching, and does not perform any security checks\. This method is utilized by \fIres\.download()\fR to transfer static files, and allows you do to so from outside of the public directory, so suitable security checks should be applied\.705.706.SS "res\.download(file[, filename])"707Transfer the given \fIfile\fR as an attachment with optional alternative \fIfilename\fR\.708.709.IP "" 4710.711.nf712713res\.download(\'path/to/image\.png\');714res\.download(\'path/to/image\.png\', \'foo\.png\');715.716.fi717.718.IP "" 0719.720.P721This is equivalent to:722.723.IP "" 4724.725.nf726727res\.attachment(file);728res\.sendfile(file);729.730.fi731.732.IP "" 0733.734.SS "res\.send(body|status[, headers|status[, status]])"735The \fBres\.send()\fR method is a high level response utility allowing you to pass objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status code based responses\. The following are all valid uses:736.737.IP "" 4738.739.nf740741 res\.send(new Buffer(\'wahoo\'));742 res\.send({ some: \'json\' });743 res\.send(\'<p>some html</p>\');744 res\.send(\'Sorry, cant find that\', 404);745 res\.send(\'text\', { \'Content\-Type\': \'text/plain\' }, 201);746 res\.send(404);747.748.fi749.750.IP "" 0751.752.P753By default the \fIContent\-Type\fR response header is set, however if explicitly assigned through \fBres\.send()\fR or previously with \fBres\.header()\fR or \fBres\.contentType()\fR it will not be set again\.754.755.SS "res\.redirect(url[, status])"756Redirect to the given \fIurl\fR with a default response \fIstatus\fR of 302\.757.758.IP "" 4759.760.nf761762res\.redirect(\'/\', 301);763res\.redirect(\'/account\');764res\.redirect(\'http://google\.com\');765res\.redirect(\'home\');766res\.redirect(\'back\');767.768.fi769.770.IP "" 0771.772.P773Express supports \"redirect mapping\", which by default provides \fIhome\fR, and \fIback\fR\. The \fIback\fR map checks the \fIReferrer\fR and \fIReferer\fR headers, while \fIhome\fR utilizes the \"home\" setting and defaults to \"/\"\.774.775.SS "res\.render(view[, options[, fn]])"776Render \fIview\fR with the given \fIoptions\fR and optional callback \fIfn\fR\. When a callback function is given a response will \fInot\fR be made automatically, however otherwise a response of \fI200\fR and \fItext/html\fR is given\.777.778.P779Most engines accept one or more of the following options, both haml \fIhttp://github\.com/visionmedia/haml\.js\fR and jade \fIhttp://github\.com/visionmedia/jade\fR accept all:780.781.IP "\(bu" 4782\fIcontext|scope\fR Template evaluation context (\fIthis\fR)783.784.IP "\(bu" 4785\fIlocals\fR Object containing local variables786.787.IP "\(bu" 4788\fIcache\fR Cache intermediate JavaScript in memory (the default in \fIproduction\fR mode)789.790.IP "\(bu" 4791\fIdebug\fR Output debugging information792.793.IP "" 0794.795.SS "res\.partial(view[, options])"796Render \fIview\fR partial with the given \fIoptions\fR\. This method is always available to the view as a local variable\.797.798.IP "\(bu" 4799\fIas\fR Variable name for each \fIcollection\fR value, defaults to the view name\.800.801.IP "\(bu" 4802as: \'something\' will add the \fIsomething\fR local variable803.804.IP "\(bu" 4805as: this will use the collection value as the template context806.807.IP "\(bu" 4808as: global will merge the collection value\'s properties with \fIlocals\fR809.810.IP "" 0811812.813.IP "\(bu" 4814\fIcollection\fR Array of objects, the name is derived from the view name itself\. For example \fIvideo\.html\fR will have a object \fIvideo\fR available to it\.815.816.IP "" 0817.818.P819The following are equivalent, and the name of collection value when passed to the partial will be \fImovie\fR as derived from the name\.820.821.IP "" 4822.823.nf824825partial(\'movie\.jade\', { collection: movies });826partial(\'movie\.jade\', movies);827partial(\'movie\', movies);828// In view: movie\.director829.830.fi831.832.IP "" 0833.834.P835To change the local from \fImovie\fR to \fIvideo\fR we can use the \"as\" option:836.837.IP "" 4838.839.nf840841partial(\'movie\', { collection: movies, as: \'video\' });842// In view: video\.director843.844.fi845.846.IP "" 0847.848.P849Also we can make our movie the value of \fIthis\fR within our view so that instead of \fImovie\.director\fR we could use \fIthis\.director\fR\.850.851.IP "" 4852.853.nf854855partial(\'movie\', { collection: movies, as: this });856// In view: this\.director857.858.fi859.860.IP "" 0861.862.P863Another alternative is to \"explode\" the properties of the collection item into pseudo globals (local variables) by using \fIas: global\fR, which again is syntactic sugar:864.865.IP "" 4866.867.nf868869partials(\'movie\', { collection: movies, as: global });870// In view: director871.872.fi873.874.IP "" 0875.876.SS "app\.set(name[, val])"877Apply an application level setting \fIname\fR to \fIval\fR, or get the value of \fIname\fR when \fIval\fR is not present:878.879.IP "" 4880.881.nf882883app\.set(\'reload views\', 200);884app\.set(\'reload views\');885// => 200886.887.fi888.889.IP "" 0890.891.SS "app\.enable(name)"892Enable the given setting \fIname\fR:893.894.IP "" 4895.896.nf897898app\.enable(\'some arbitrary setting\');899app\.set(\'some arbitrary setting\');900// => true901.902.fi903.904.IP "" 0905.906.SS "app\.disable(name)"907Disable the given setting \fIname\fR:908.909.IP "" 4910.911.nf912913app\.disable(\'some setting\');914app\.set(\'some setting\');915// => false916.917.fi918.919.IP "" 0920.921.SS "app\.configure(env|function[, function])"922Define a callback function for the given \fIenv\fR (or all environments) with callback \fIfunction\fR:923.924.IP "" 4925.926.nf927928app\.configure(function(){929    // executed for each env930});931932app\.configure(\'development\', function(){933    // executed for \'development\' only934});935.936.fi937.938.IP "" 0939.940.SS "app\.redirect(name, val)"941For use with \fBres\.redirect()\fR we can map redirects at the application level as shown below:942.943.IP "" 4944.945.nf946947app\.redirect(\'google\', \'http://google\.com\');948.949.fi950.951.IP "" 0952.953.P954Now in a route we may call:955.956.P957res\.redirect(\'google\');958.959.P960We may also map dynamic redirects:961.962.IP "" 4963.964.nf965966app\.redirect(\'comments\', function(req, res){967    return \'/post/\' + req\.params\.id + \'/comments\';968});969.970.fi971.972.IP "" 0973.974.P975So now we may do the following, and the redirect will dynamically adjust to the context of the request\. If we called this route with \fIGET /post/12\fR our redirect \fILocation\fR would be \fI/post/12/comments\fR\.976.977.IP "" 4978.979.nf980981app\.get(\'/post/:id\', function(req, res){982    res\.redirect(\'comments\');983});984.985.fi986.987.IP "" 0988.989.SS "app\.error(function)"990Adds an error handler \fIfunction\fR which will receive the exception as the first parameter as shown below\. Note that we may set several error handlers by making several calls to this method, however the handler should call \fInext(err)\fR if it does not wish to deal with the exception:991.992.IP "" 4993.994.nf995996app\.error(function(err, req, res, next){997    res\.send(err\.message, 500);998});999.1000.fi1001.1002.IP "" 01003.1004.SS "app\.helpers(obj)"1005Registers static view helpers\.1006.1007.IP "" 41008.1009.nf10101011app\.helpers({1012    name: function(first, last){ return first + \', \' + last },1013    firstName: \'tj\',1014    lastName: \'holowaychuk\'1015});1016.1017.fi1018.1019.IP "" 01020.1021.P1022Our view could now utilize the \fIfirstName\fR and \fIlastName\fR variables, as well as the \fIname()\fR function exposed\.1023.1024.IP "" 41025.1026.nf10271028<%= name(firstName, lastName) %>1029.1030.fi1031.1032.IP "" 01033.1034.SS "app\.dynamicHelpers(obj)"1035Registers dynamic view helpers\. Dynamic view helpers are simply functions which accept \fIreq\fR, \fIres\fR, and are evaluated against the \fIServer\fR instance before a view is rendered\. The \fIreturn value\fR of this function becomes the local variable it is associated with\.1036.1037.IP "" 41038.1039.nf10401041app\.dynamicHelpers({1042    session: function(req, res){1043        return req\.session;1044    }1045});1046.1047.fi1048.1049.IP "" 01050.1051.P1052All views would now have \fIsession\fR available so that session data can be accessed via \fIsession\.name\fR etc:1053.1054.IP "" 41055.1056.nf10571058<%= session\.name %>1059.1060.fi1061.1062.IP "" 01063.1064.SS "app\.mounted(fn)"1065Assign a callback \fIfn\fR which is called when this \fIServer\fR is passed to \fIServer#use()\fR\.1066.1067.IP "" 41068.1069.nf10701071var app = express\.createServer(),1072    blog = express\.createServer();10731074blog\.mounted(function(parent){1075    // parent is app1076    // \"this\" is blog1077});10781079app\.use(blog);1080.1081.fi1082.1083.IP "" 01084.1085.SS "app\.listen([port[, host]])"1086Bind the app server to the given \fIport\fR, which defaults to 3000\. When \fIhost\fR is omitted all connections will be accepted via \fIINADDR_ANY\fR\.1087.1088.IP "" 41089.1090.nf10911092app\.listen();1093app\.listen(3000);1094app\.listen(3000, \'n\.n\.n\.n\');1095.1096.fi1097.1098.IP "" 01099.1100.P1101The \fIport\fR argument may also be a string representing the path to a unix domain socket:1102.1103.IP "" 41104.1105.nf11061107app\.listen(\'/tmp/express\.sock\');1108.1109.fi1110.1111.IP "" 01112.1113.P1114Then try it out:1115.1116.IP "" 41117.1118.nf11191120$ telnet /tmp/express\.sock1121GET / HTTP/1\.111221123HTTP/1\.1 200 OK1124Content\-Type: text/plain1125Content\-Length: 1111261127Hello World1128.1129.fi1130.1131.IP "" 01132