Skip to content
Myr

application.md

This file

Path

docs/application.md

181 lines · 4.5 KiB · Markdown

Last changed by

app.engine() docsTJ Holowaychukbefore adoptionfab3e91

History of this file

Take it

The walk stopped after 200 commits: lines older than b4cd6bb point at it.

removed old route reflection methodsTJ Holowaychukbefore adoptionb4cd6bb

lines 1–13why, one step back
1
2# app34 Application prototype.56# app.use()78 Proxy `connect#use()` to apply settings to9 mounted applications.1011# app.engine()1213 Register the given template engine callback `fn`

app.engine() docsTJ Holowaychukbefore adoptionfab3e91

line 14why, one step back
14 as `ext`.

removed old route reflection methodsTJ Holowaychukbefore adoptionb4cd6bb

line 15why, one step back
15

app.engine() docsTJ Holowaychukbefore adoptionfab3e91

lines 16–26why, one step back
16 By default will `require()` the engine based on the
17 file extension. For example if you try to render18 a "foo.jade" file Express will invoke the following internally:19 20 app.engine('jade', require('jade').__express);21 22 For engines that do not provide `.__express` out of the box,23 or if you wish to "map" a different extension to the template engine24 you may use this method. For example mapping the EJS template engine to25 ".html" files26

removed old route reflection methodsTJ Holowaychukbefore adoptionb4cd6bb

lines 27–29why, one step back
27 app.engine('html', require('ejs').renderFile);
28 29 In this case EJS provides a `.renderFile()` method with

app.engine() docsTJ Holowaychukbefore adoptionfab3e91

lines 30–32why, one step back
30 the same signature that Express expects: `(path, options, callback)`,
31 though note that it aliases this method as `ejs.__express` internally32 so if you're using ".ejs" extensions you dont need to do anything.

removed old route reflection methodsTJ Holowaychukbefore adoptionb4cd6bb

lines 33–181why, one step back
33
34 Some template engines do not follow this convention, the35 [Consolidate.js](https://github.com/visionmedia/consolidate.js)36 library was created to map all of node's popular template37 engines to follow this convention, thus allowing them to38 work seemlessly within Express.3940# app.param()4142 Map the given param placeholder `name`(s) to the given callback(s).43 44 Parameter mapping is used to provide pre-conditions to routes45 which use normalized placeholders. For example a _:user_id_ parameter46 could automatically load a user's information from the database without47 any additional code,48 49 The callback uses the samesignature as middleware, the only differencing50 being that the value of the placeholder is passed, in this case the _id_51 of the user. Once the `next()` function is invoked, just like middleware52 it will continue on to execute the route, or subsequent parameter functions.53 54 app.param('user_id', function(req, res, next, id){55 User.find(id, function(err, user){56 if (err) {57 next(err);58 } else if (user) {59 req.user = user;60 next();61 } else {62 next(new Error('failed to load user'));63 }64 });65 });6667# app.set()6869 Assign `setting` to `val`, or return `setting`'s value.70 71 app.set('foo', 'bar');72 app.get('foo');73 // => "bar"74 75 Mounted servers inherit their parent server's settings.7677# app.enabled()7879 Check if `setting` is enabled (truthy).80 81 app.enabled('foo')82 // => false83 84 app.enable('foo')85 app.enabled('foo')86 // => true8788# app.disabled()8990 Check if `setting` is disabled.91 92 app.disabled('foo')93 // => true94 95 app.enable('foo')96 app.disabled('foo')97 // => false9899# app.enable()100101 Enable `setting`.102103# app.disable()104105 Disable `setting`.106107# app.configure()108109 Configure callback for zero or more envs,110 when no `env` is specified that callback will111 be invoked for all environments. Any combination112 can be used multiple times, in any order desired.113 114 ## Examples115 116 app.configure(function(){117 // executed for all envs118 });119 120 app.configure('stage', function(){121 // executed staging env122 });123 124 app.configure('stage', 'production', function(){125 // executed for stage and production126 });127 128 ## Note129 130 These callbacks are invoked immediately, and131 are effectively sugar for the following.132 133 var env = process.env.NODE_ENV || 'development';134 135 switch (env) {136 case 'development':137 ...138 break;139 case 'stage':140 ...141 break;142 case 'production':143 ...144 break;145 }146147# app.all()148149 Special-cased "all" method, applying the given route `path`,150 middleware, and callback to _every_ HTTP method.151152# app.render()153154 Render the given view `name` name with `options`155 and a callback accepting an error and the156 rendered template string.157 158 ## Example159 160 app.render('email', { name: 'Tobi' }, function(err, html){161 // ...162 })163164# app.listen()165166 Listen for connections.167 168 A node `http.Server` is returned, with this169 application (which is a `Function`) as its170 callback. If you wish to create both an HTTP171 and HTTPS server you may do so with the "http"172 and "https" modules as shown here.173 174 var http = require('http')175 , https = require('https')176 , express = require('express')177 , app = express();178 179 http.createServer(app).listen(80);180 http.createServer({ ... }, app).listen(443);181