application.md
This file
Path
docs/application.md
170 lines · 4.0 KiB · Markdown
Last changed by
Added new dox API docsTJ Holowaychukbefore adoption7e81cad
History of this fileTake it
Added new dox API docsTJ Holowaychukbefore adoption7e81cad
lines 1–170why, one step back12# 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`14 as `ext`. For example if you wanted to map the EJS15 template engine to ".html" files, rather than ".ejs" files,16 you could do the following.17 18 app.engine('html', require('ejs').renderFile);19 20 In this case EJS provides a `.renderFile()` method with21 the same signature that Express expects: `(path, options, callback)`.22 23 Some template engines do not follow this convention, the24 [Consolidate.js](https://github.com/visionmedia/consolidate.js)25 library was created to map all of node's popular template26 engines to follow this convention, thus allowing them to27 work seemlessly within Express.2829# app.param()3031 Map the given param placeholder `name`(s) to the given callback(s).32 33 Parameter mapping is used to provide pre-conditions to routes34 which use normalized placeholders. For example a _:user_id_ parameter35 could automatically load a user's information from the database without36 any additional code,37 38 The callback uses the samesignature as middleware, the only differencing39 being that the value of the placeholder is passed, in this case the _id_40 of the user. Once the `next()` function is invoked, just like middleware41 it will continue on to execute the route, or subsequent parameter functions.42 43 app.param('user_id', function(req, res, next, id){44 User.find(id, function(err, user){45 if (err) {46 next(err);47 } else if (user) {48 req.user = user;49 next();50 } else {51 next(new Error('failed to load user'));52 }53 });54 });5556# app.set()5758 Assign `setting` to `val`, or return `setting`'s value.59 60 app.set('foo', 'bar');61 app.get('foo');62 // => "bar"63 64 Mounted servers inherit their parent server's settings.6566# app.enabled()6768 Check if `setting` is enabled (truthy).69 70 app.enabled('foo')71 // => false72 73 app.enable('foo')74 app.enabled('foo')75 // => true7677# app.disabled()7879 Check if `setting` is disabled.80 81 app.disabled('foo')82 // => true83 84 app.enable('foo')85 app.disabled('foo')86 // => false8788# app.enable()8990 Enable `setting`.9192# app.disable()9394 Disable `setting`.9596# app.configure()9798 Configure callback for zero or more envs,99 when no `env` is specified that callback will100 be invoked for all environments. Any combination101 can be used multiple times, in any order desired.102 103 ## Examples104 105 app.configure(function(){106 // executed for all envs107 });108 109 app.configure('stage', function(){110 // executed staging env111 });112 113 app.configure('stage', 'production', function(){114 // executed for stage and production115 });116 117 ## Note118 119 These callbacks are invoked immediately, and120 are effectively sugar for the following.121 122 var env = process.env.NODE_ENV || 'development';123 124 switch (env) {125 case 'development':126 ...127 break;128 case 'stage':129 ...130 break;131 case 'production':132 ...133 break;134 }135136# app.all()137138 Special-cased "all" method, applying the given route `path`,139 middleware, and callback to _every_ HTTP method.140141# app.render()142143 Render the given view `name` name with `options`144 and a callback accepting an error and the145 rendered template string.146 147 ## Example148 149 app.render('email', { name: 'Tobi' }, function(err, html){150 // ...151 })152153# app.listen()154155 Listen for connections.156 157 A node `http.Server` is returned, with this158 application (which is a `Function`) as its159 callback. If you wish to create both an HTTP160 and HTTPS server you may do so with the "http"161 and "https" modules as shown here.162 163 var http = require('http')164 , https = require('https')165 , express = require('express')166 , app = express();167 168 http.createServer(app).listen(80);169 http.createServer({ ... }, app).listen(443);170