guide.md
This file
Path
docs/guide.md
642 lines · 18.7 KiB · Markdown
Last changed by
Updated docsTj Holowaychukbefore adoption0111582
History of this fileTake it
Started guideTj Holowaychukbefore adoption9e3a0c6
line 1why, one step back1Install docsTj Holowaychukbefore adoption63d02f1
lines 2–3why, one step back2### Installation3Updated curl install docsTj Holowaychukbefore adoption3ceb0de
lines 4–7why, one step back4curl (or similar):56 $ curl -# http://expressjs.com/install.sh | sh7Install docsTj Holowaychukbefore adoption63d02f1
lines 8–9why, one step back8npm:9Updated install docsTj Holowaychukbefore adoption193c4a9
line 10why, one step back10 $ npm install connectInstall docsTj Holowaychukbefore adoption63d02f1
lines 11–12why, one step back11 $ npm install express12Updated install docsTj Holowaychukbefore adoption193c4a9
line 13why, one step back13git clone, first update the submodules:Install docsTj Holowaychukbefore adoption63d02f1
line 14why, one step back14Updated install docsTj Holowaychukbefore adoption193c4a9
line 15why, one step back15 $ git submodule update --initInstall docsTj Holowaychukbefore adoption63d02f1
line 16why, one step back16 $ make installUpdated install docsTj Holowaychukbefore adoption193c4a9
line 17why, one step back17 $ make install-supportInstall docsTj Holowaychukbefore adoption63d02f1
line 18why, one step back18Removed annoying padding transitionTj Holowaychukbefore adoption346b2a2
line 19why, one step back19### Creating An ApplicationAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 20–21why, one step back2021The _express.Server_ now inherits from _http.Server_, howeverMoved config docs upTj Holowaychukbefore adoption6e8413f
lines 22–23why, one step back22follows the same idiom by providing _express.createServer()_ as shown below. This means23that you can utilize Express server's transparently with other libraries.Added configuration sectionTj Holowaychukbefore adoption83d2555
lines 24–32why, one step back2425 var app = require('express').createServer();26 27 app.get('/', function(req, res){28 res.send('hello world');29 });30 31 app.listen(3000);32Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 33–43why, one step back33### Configuration3435Express supports arbitrary environments, such as _production_ and _development_. Developers36can use the _configure()_ method to setup needs required by the current environment. When37_configure()_ is called without an environment name it will be run in _every_ environment 38prior to the environment specific callback.3940In the example below we only _dumpExceptions_, and respond with exception stack traces41in _development_ mode, however for both environments we utilize _methodOverride_ and _bodyDecoder_.4243 app.configure(function(){Updated docsTj Holowaychukbefore adoptioncacada8
lines 44–45why, one step back44 app.use(connect.methodOverride());45 app.use(connect.bodyDecoder());Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 46–48why, one step back46 });47 48 app.configure('development', function(){Updated docsTj Holowaychukbefore adoptioncacada8
line 49why, one step back49 app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 50–52why, one step back50 });51 52 app.configure('production', function(){Updated docsTj Holowaychukbefore adoptioncacada8
line 53why, one step back53 app.use(connect.errorHandler());Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 54–69why, one step back54 });5556For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:5758 app.configure(function(){59 app.set('views', __dirname + '/views');60 app.set('views');61 // => "... views directory ..."62 63 app.enable('some feature');64 // same as app.set('some feature', true);65 66 app.disable('some feature');67 // same as app.set('some feature', false);68 });69More docsTj Holowaychukbefore adoptionde81ee7
lines 70–74why, one step back70To alter the environment we can set the _CONNECT_ENV_ environment variable,71or more specifically _EXPRESS_ENV_, for example:7273 $ EXPRESS_ENV=production node app.js74Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 75–78why, one step back75### Settings7677Express supports the following settings out of the box:78Updated docsTj Holowaychukbefore adoption4d32762
lines 79–81why, one step back79 * _env_ Application environment set internally, use _app.set('env')_ on _Server#listen()_80 * _home_ Application base path used with _res.redirect()_, when mounted defaults81 to _Server#route_, otherwise "/". Assigned on _Server#listen()_.Moved config docs upTj Holowaychukbefore adoption6e8413f
lines 82–83why, one step back82 * _views_ Root views directory defaulting to **CWD/views**83 * _view engine_ Default view engine name for views rendered without extensionsAdded more setting docsTj Holowaychukbefore adoption38281e6
line 84why, one step back84 * _reload views_ Reloads altered views, by default watches for _mtime_ changes withFixed reload views exampleTj Holowaychukbefore adoption5fcf971
line 85why, one step back85 with a 5 minute interval. Example: _app.set('reload views', 60000);_Moved config docs upTj Holowaychukbefore adoption6e8413f
line 86why, one step back86Started guideTj Holowaychukbefore adoption9e3a0c6
lines 87–90why, one step back87### Routing8889Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.90For example we may want to render a user's account for the path _/user/12_, thisRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 91–92why, one step back91can be done by defining the route below. The values associated to the named placeholders,92are passed as the _third_ argument, which here we name _params_.Started guideTj Holowaychukbefore adoption9e3a0c6
lines 93–102why, one step back9394 app.get('/user/:id', function(req, res, params){95 res.send('user ' + params.id);96 });9798A route is simple a string which is compiled to a _RegExp_ internally. For example99when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:100101 \/user\/([^\/]+)\/?102Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b
line 103why, one step back103Regular expression literals may also be passed for complex uses. Since captureUpdated docsTj Holowaychukbefore adoption0111582
line 104why, one step back104groups with literal _RegExp_'s are anonymous we can access them directly from the _params_ array.Started guideTj Holowaychukbefore adoption9e3a0c6
line 105why, one step back105Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b
line 106why, one step back106 app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function(req, res, params){Updated docsTj Holowaychukbefore adoption0111582
line 107why, one step back107 res.send(params);Docs for params.captures. Closes #358Tj Holowaychukbefore adoptiond03821b
lines 108–119why, one step back108 });109110Curl requests against the previously defined route:111112 $ curl http://dev:3000/user113 [null,null]114 $ curl http://dev:3000/users115 [null,null]116 $ curl http://dev:3000/users/1117 ["1",null]118 $ curl http://dev:3000/users/1..15119 ["1","15"]Started guideTj Holowaychukbefore adoption9e3a0c6
lines 120–150why, one step back120121Below are some route examples, and the associated paths that they122may consume:123124 "/user/:id"125 /user/12126127 "/users/:id?"128 /users/5129 /users130131 "/files/*"132 /files/jquery.js133 /files/javascripts/jquery.js134135 "/file/*.*"136 /files/jquery.js137 /files/javascripts/jquery.js138 139 "/user/:id/:operation?"140 /user/1141 /user/1/edit142 143 "/products.:format"144 /products.json145 /products.xml146147 "/products.:format?"148 /products.json149 /products.xml150 /productsRoute passing docsTj Holowaychukbefore adoption0ace97c
lines 151–166why, one step back151152### Passing Route Control153154We may pass control to the next _matching_ route, by calling the _fourth_ parameter,155the _next()_ function. When a match cannot be made, control is passed back to Connect.156157 app.get('/users/:id?', function(req, res, params){158 if (params.id) {159 // do something160 } else {161 next();162 }163 });164 165 app.get('/users', function(req, res, params){166 // do something elseAdded configuration sectionTj Holowaychukbefore adoption83d2555
lines 167–181why, one step back167 });168169### Middleware170171The Express _Plugin_ is no more! middleware via [Connect](http://github.com/extjs/Connect) can be172passed to _express.createServer()_ as you would with a regular Connect server. For example:173174 var connect = require('connect'),175 express = require('express');176177 var app = express.createServer(178 connect.logger(),179 connect.bodyDecoder()180 );181Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 182–183why, one step back182Alternatively we can _use()_ them which is useful when adding middleware within _configure()_ blocks:183Updated docsTj Holowaychukbefore adoptioncacada8
line 184why, one step back184 app.use(connect.logger({ format: ':method :uri' }));Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 185–230why, one step back185186### Error Handling187188Express provides the _app.error()_ method which receives exceptions thrown within a route,189or passed to _next(err)_. Below is an example which serves different pages based on our190ad-hoc _NotFound_ exception:191192 function NotFound(msg){193 this.name = 'NotFound';194 Error.call(this, msg);195 Error.captureStackTrace(this, arguments.callee);196 }197198 sys.inherits(NotFound, Error);199200 app.get('/404', function(req, res){201 throw new NotFound;202 });203204 app.get('/500', function(req, res){205 throw new Error('keyboard cat!');206 });207208We can call _app.error()_ several times as shown below.209Here we check for an instanceof _NotFound_ and show the210404 page, or we pass on to the next error handler.211212 app.error(function(err, req, res, next){213 if (err instanceof NotFound) {214 res.render('404.jade');215 } else {216 next(err);217 }218 });219220Here we assume all errors as 500 for the simplicity of221this demo, however you can choose whatever you like222223 app.error(function(err, req, res){224 res.render('500.jade', {225 locals: {226 error: err227 } 228 });229 });230More error handling docsTj Holowaychukbefore adoptioncea401c
lines 231–234why, one step back231Our apps could also utilize the Connect _errorHandler_ middleware232to report on exceptions. For example if we wish to output exceptions 233in "development" mode to _stderr_ we can use:234Updated docsTj Holowaychukbefore adoptioncacada8
line 235why, one step back235 app.use(connect.errorHandler({ dumpExceptions: true }));More error handling docsTj Holowaychukbefore adoptioncea401c
lines 236–239why, one step back236237Also during development we may want fancy html pages to show exceptions238that are passed or thrown, so we can set _showStack_ to true:239Updated docsTj Holowaychukbefore adoptioncacada8
line 240why, one step back240 app.use(connect.errorHandler({ showStack: true, dumpExceptions: true }));More error handling docsTj Holowaychukbefore adoptioncea401c
lines 241–244why, one step back241242The _errorHandler_ middleware also responds with _json_ if _Accept: application/json_243is present, which is useful for developing apps that rely heavily on client-side JavaScript.244Added more view docsTj Holowaychukbefore adoption2f96506
lines 245–262why, one step back245### View Rendering246247View filenames take the form _NAME_._ENGINE_, where _ENGINE_ is the name248of the module that will be required. For example the view _layout.ejs_ will249tell the view system to _require('ejs')_, the module being loaded must (currently)250export the method _exports.render(str, options)_ to comply with Express, however 251with will likely be extensible in the future.252253Below is an example using [Haml.js](http://github.com/visionmedia/haml.js) to render _index.html_,254and since we do not use _layout: false_ the rendered contents of _index.html_ will be passed as 255the _body_ local variable in _layout.haml_.256257 app.get('/', function(req, res){258 res.render('index.haml', {259 locals: { title: 'My Site' }260 });261 });262Docs for "view engine" settingTj Holowaychukbefore adoption1810c9b
lines 263–280why, one step back263The new _view engine_ setting allows us to specify our default template engine,264so for example when using [Jade](http://github.com/visionmedia/jade) we could set:265266 app.set('view engine', 'jade');267268Allowing us to render with:269270 res.render('index');271272vs:273274 res.render('index.jade');275276When _view engine_ is set, extensions are entirely optional, however we can still277mix and match template engines:278279 res.render('another-page.ejs');280Added more view docsTj Holowaychukbefore adoption2f96506
lines 281–298why, one step back281### View Partials282283The Express view system has built-in support for partials and collections, which are284sort of "mini" views representing a document fragment. For example rather than iterating285in a view to display comments, we would use a partial with collection support:286287 partial('comment.haml', { collection: comments });288289To make things even less verbose we can assume the extension as _.haml_ when omitted,290however if we wished we could use an ejs partial, within a haml view for example.291292 partial('comment', { collection: comments });293294And once again even further, when rendering a collection we can simply pass295an array, if no other options are desired:296297 partial('comments', comments);298Added partial magic var docs. Closes #342Tj Holowaychukbefore adoptionbe85701
lines 299–305why, one step back299When using the partial collection support a few "magic" variables are provided300for free:301302 * _firstInCollection_ True if this is the first object303 * _indexInCollection_ Index of the object in the collection304 * _lastInCollection _ True if this is the last object305Added more view docsTj Holowaychukbefore adoption2f96506
lines 306–313why, one step back306### Template Engines307308Below are a few template engines commonly used with Express:309310 * [Jade](http://github.com/visionmedia/jade) haml.js successor311 * [Haml](http://github.com/visionmedia/haml.js) indented templates312 * [EJS](http://github.com/visionmedia/ejs) Embedded JavaScript313More docsTj Holowaychukbefore adoptionde81ee7
line 314why, one step back314### req.header(key[, defaultValue])More docsTj Holowaychukbefore adoptiondfecbe5
lines 315–321why, one step back315316Get the case-insensitive request header _key_, with optional _defaultValue_:317318 req.header('Host');319 req.header('host');320 req.header('Accept', '*/*');321More docsTj Holowaychukbefore adoptionde81ee7
line 322why, one step back322### req.accepts(type)More docsTj Holowaychukbefore adoptiondfecbe5
lines 323–345why, one step back323324Check if the _Accept_ header is present, and includes the given _type_.325326When the _Accept_ header is not present _true_ is returned. Otherwise327the given _type_ is matched by an exact match, and then subtypes. You328may pass the subtype such as "html" which is then converted internally329to "text/html" using the mime lookup table.330331 // Accept: text/html332 req.accepts('html');333 // => true334335 // Accept: text/*; application/json336 req.accepts('html');337 req.accepts('text/html');338 req.accepts('text/plain');339 req.accepts('application/json');340 // => true341342 req.accepts('image/png');343 req.accepts('png');344 // => false345More docsTj Holowaychukbefore adoptionde81ee7
line 346why, one step back346### req.param(name)More docsTj Holowaychukbefore adoptiondfecbe5
lines 347–357why, one step back347348Return the value of param _name_ when present.349350 - Checks route placeholders, ex: /user/:id351 - Checks query string params, ex: ?id=12352 - Checks urlencoded body params, ex: id=12353354To utilize urlencoded request bodies, _req.body_355should be an object. This can be done by using356the _connect.bodyDecoder_ middleware.357More docsTj Holowaychukbefore adoptionde81ee7
line 358why, one step back358### req.flash(type[, msg])More docsTj Holowaychukbefore adoptiondfecbe5
lines 359–375why, one step back359360Queue flash _msg_ of the given _type_.361362 req.flash('info', 'email sent');363 req.flash('error', 'email delivery failed');364 req.flash('info', 'email re-sent');365 // => 2366367 req.flash('info');368 // => ['email sent', 'email re-sent']369370 req.flash('info');371 // => []372373 req.flash();374 // => { error: ['email delivery failed'], info: [] }375More docsTj Holowaychukbefore adoptionde81ee7
line 376why, one step back376### req.isXMLHttpRequestMore docsTj Holowaychukbefore adoptiondfecbe5
lines 377–383why, one step back377378Also aliased as _req.xhr_, this getter checks the _X-Requested-With_ header379to see if it was issued by an _XMLHttpRequest_:380381 req.xhr382 req.isXMLHttpRequest383More docsTj Holowaychukbefore adoptionde81ee7
line 384why, one step back384### res.header(key[, val])More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 385–396why, one step back385386Get or set the response header _key_.387388 res.header('Content-Length');389 // => undefined390391 res.header('Content-Length', 123);392 // => 123393394 res.header('Content-Length');395 // => 123396More docsTj Holowaychukbefore adoptionde81ee7
line 397why, one step back397### res.contentType(type)More docs :)Tj Holowaychukbefore adoption0c5fe61
lines 398–404why, one step back398399Sets the _Content-Type_ response header to the given _type_.400401 var filename = 'path/to/image.png';402 res.contentType(filename);403 // res.headers['Content-Type'] is now "image/png"404More docsTj Holowaychukbefore adoptionde81ee7
line 405why, one step back405### res.attachment([filename])Docs for res.attachment()Tj Holowaychukbefore adoptioneb235e1
lines 406–410why, one step back406407Sets the _Content-Disposition_ response header to "attachment", with optional _filename_.408409 res.attachment('path/to/my/image.png');410More docsTj Holowaychukbefore adoptionde81ee7
line 411why, one step back411### res.sendfile(path)Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 412–416why, one step back412413Used by `res.download()` to transfer an arbitrary file. 414415 res.sendfile('path/to/my.file');416Better docs for res.sendfile()Tj Holowaychukbefore adoption8a337f1
lines 417–420why, one step back417This is _not_ a substitution for Connect's _staticProvider_ middleware, it does not418support HTTP caching, and does not perform any security checks. This method is utilized419by _res.download()_ to transfer static files, and allows you do to so from outside of420the public directory, so suitable security checks should be applied.Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
line 421why, one step back421More docsTj Holowaychukbefore adoptionde81ee7
line 422why, one step back422### res.download(file[, filename])Docs for new res methodsTj Holowaychukbefore adoptionb08dab9
lines 423–433why, one step back423424Transfer the given _file_ as an attachment with optional alternative _filename_.425426 res.download('path/to/image.png');427 res.download('path/to/image.png', 'foo.png');428429This is equivalent to:430431 res.attachment(file);432 res.sendfile(file);433More docsTj Holowaychukbefore adoptionde81ee7
line 434why, one step back434### res.send(body|status[, headers|status[, status]])Docs for res.send()Tj Holowaychukbefore adoption5a48248
lines 435–450why, one step back435436The `res.send()` method is a high level response utility allowing you to pass437objects to respond with json, strings for html, arbitrary _Buffer_s or numbers for status438code based responses. The following are all valid uses:439440 res.send(new Buffer('wahoo'));441 res.send({ some: 'json' });442 res.send('<p>some html</p>');443 res.send('Sorry, cant find that', 404);444 res.send('text', { 'Content-Type': 'text/plain' }, 201);445 res.send(404);446447By default the _Content-Type_ response header is set, however if explicitly448assigned through `res.send()` or previously with `res.header()` or `res.contentType()`449it will not be set again.450More docsTj Holowaychukbefore adoptionde81ee7
line 451why, one step back451### res.redirect(url[, status])Added configuration sectionTj Holowaychukbefore adoption83d2555
line 452why, one step back452More docsTj Holowaychukbefore adoptionbedff32
line 453why, one step back453Redirect to the given _url_ with a default response _status_ of 302.Configuration docsTj Holowaychukbefore adoptioncb04364
line 454why, one step back454More docsTj Holowaychukbefore adoptionbedff32
lines 455–464why, one step back455 res.redirect('/', 301);456 res.redirect('/account');457 res.redirect('http://google.com');458 res.redirect('home');459 res.redirect('back');460461Express supports "redirect mapping", which by default provides _home_, and _back_.462The _back_ map checks the _Referrer_ and _Referer_ headers, while _home_ utilizes463the "home" setting and defaults to "/".464More view docsTj Holowaychukbefore adoption1c8ff48
lines 465–475why, one step back465### res.render(view[, options[, fn]])466467Render _view_ with the given _options_ and optional callback _fn_.468When a callback function is given a response will _not_ be made469automatically, however otherwise a response of _200_ and _text/html_ is given.470471 Most engines accept one or more of the following options,472 both [haml](http://github.com/visionmedia/haml.js) and [jade](http://github.com/visionmedia/jade) accept all:473474 - _context|scope_ Template evaluation context (_this_)475 - _locals_ Object containing local variablesCleaning up view docsTj Holowaychukbefore adoption25c8883
line 476why, one step back476 - _cache_ Cache intermediate JavaScript in memory (the default in _production_ mode)More view docsTj Holowaychukbefore adoption1c8ff48
lines 477–478why, one step back477 - _debug_ Output debugging information478In depth partial docsTj Holowaychukbefore adoption793943f
lines 479–516why, one step back479### res.partial(view[, options])480481Render _view_ partial with the given _options_. This method is always available482to the view as a local variable.483484- _as_ Variable name for each _collection_ value, defaults to the view name.485 * as: 'something' will add the _something_ local variable486 * as: this will use the collection value as the template context487 * as: global will merge the collection value's properties with _locals_488489- _collection_ Array of objects, the name is derived from the view name itself. 490 For example _video.html_ will have a object _video_ available to it.491492The following are equivalent, and the name of collection value when passed493to the partial will be _movie_ as derived from the name.494495 partial('movie.jade', { collection: movies });496 partial('movie.jade', movies);497 partial('movie', movies);498 // In view: movie.director499500To change the local from _movie_ to _video_ we can use the "as" option:501502 partial('movie', { collection: movies, as: 'video' });503 // In view: video.director504505Also we can make our movie the value of _this_ within our view so that instead506of _movie.director_ we could use _this.director_.507508 partial('movie', { collection: movies, as: this });509 // In view: this.director510511Another alternative is to "explode" the properties of the collection item into512pseudo globals (local variables) by using _as: global_, which again is syntactic sugar:513514 partials('movie', { collection: movies, as: global });515 // In view: director516More docsTj Holowaychukbefore adoptionde81ee7
lines 517–554why, one step back517### app.set(name[, val])518519Apply an application level setting _name_ to _val_, or520get the value of _name_ when _val_ is not present:521522 app.set('reload views', 200);523 app.set('reload views');524 // => 200525526### app.enable(name)527528Enable the given setting _name_:529530 app.enable('some arbitrary setting');531 app.set('some arbitrary setting');532 // => true533534### app.disable(name)535536Disable the given setting _name_:537538 app.disable('some setting');539 app.set('some setting');540 // => false541542### app.configure(env|function[, function])543544Define a callback function for the given _env_ (or all environments) with callback _function_:545546 app.configure(function(){547 // executed for each env548 });549550 app.configure('development', function(){551 // executed for 'development' only552 });553554### app.redirect(name, val)More docsTj Holowaychukbefore adoptionbedff32
lines 555–576why, one step back555556For use with `res.redirect()` we can map redirects at the application level as shown below:557558 app.redirect('google', 'http://google.com');559560Now in a route we may call:561562 res.redirect('google');563564We may also map dynamic redirects:565566 app.redirect('comments', function(req, res, params){567 return '/post/' + params.id + '/comments';568 });569570So now we may do the following, and the redirect will dynamically adjust to571the context of the request. If we called this route with _GET /post/12_ our572redirect _Location_ would be _/post/12/comments_.573574 app.get('/post/:id', function(req, res){575 res.redirect('comments');576 });Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 577–579why, one step back577578### app.error(function)579More error docsTj Holowaychukbefore adoption6227f5c
lines 580–582why, one step back580Adds an error handler _function_ which will receive the exception as the first parameter as shown below.581Note that we may set several error handlers by making several calls to this method, however the handler582should call _next(err)_ if it does not wish to deal with the exception:Error handling docsTj Holowaychukbefore adoption9fbb9fa
lines 583–586why, one step back583584 app.error(function(err, req, res, next){585 res.send(err.message, 500);586 });Added app.helpers() docsTj Holowaychukbefore adoption7eb088b
lines 587–589why, one step back587588### app.helpers(obj)589Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1
lines 590–605why, one step back590Registers static view helpers.591592 app.helpers({593 name: function(first, last){ return first + ', ' + last },594 firstName: 'tj',595 lastName: 'holowaychuk'596 });597598Our view could now utilize the _firstName_ and _lastName_ variables,599as well as the _name()_ function exposed.600601 <%= name(firstName, lastName) %>602603### app.dynamicHelpers(obj)604605Registers dynamic view helpers. Dynamic view helpersAdded app.helpers() docsTj Holowaychukbefore adoption7eb088b
line 606why, one step back606are simply functions which accept _req_, _res_, and _params_, and areDocs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1
line 607why, one step back607evaluated against the _Server_ instance before a view is rendered. The _return value_ of this functionAdded app.helpers() docsTj Holowaychukbefore adoption7eb088b
lines 608–609why, one step back608becomes the local variable it is associated with.609Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1
line 610why, one step back610 app.dynamicHelpers({Added app.helpers() docsTj Holowaychukbefore adoption7eb088b
lines 611–615why, one step back611 session: function(req, res, params){612 return req.session;613 }614 });615Docs for Server#{helpers,dynamicHelpers}()Tj Holowaychukbefore adoption78008d1
lines 616–618why, one step back616All views would now have _session_ available so that session data can be accessed via _session.name_ etc:617618 <%= session.name %>Server#listen() docsTj Holowaychukbefore adoptionf3803c4
lines 619–627why, one step back619620### app.listen([port[, host]])621622Bind the app server to the given _port_, which defaults to 3000. When _host_ is omitted all623connections will be accepted via _INADDR_ANY_.624625 app.listen();626 app.listen(3000);627 app.listen(3000, 'n.n.n.n');Unix domain socket docsTj Holowaychukbefore adoption2fa7907
lines 628–642why, one step back628629The _port_ argument may also be a string representing the path to a unix domain socket:630631 app.listen('/tmp/express.sock');632633Then try it out:634635 $ telnet /tmp/express.sock636 GET / HTTP/1.1637638 HTTP/1.1 200 OK639 Content-Type: text/plain640 Content-Length: 11641 642 Hello World