Skip to content
Myr

Added docs/express.md makefile target

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 0ddc40a · Browse files · 2 files+503 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+503 added−1 removed

  • other:+503 added−1 removed

2 files+503 added−1 removed

added:docs/express.md+499 added
12# 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);170171172# req173174 Request prototype.175176# req.get()177178 Return request header.179 180 The `Referrer` header field is special-cased,181 both `Referrer` and `Referer` will yield are182 interchangeable.183 184 ## Examples185 186 req.get('Content-Type');187 // => "text/plain"188 189 req.get('content-type');190 // => "text/plain"191 192 req.get('Something');193 // => undefined194195# req.accepts()196197 Check if the given `type(s)` is acceptable, returning198 the best match when true, otherwise `undefined`, in which199 case you should respond with 406 "Not Acceptable".200 201 The `type` value may be a single mime type string202 such as "application/json", the extension name203 such as "json", a comma-delimted list such as "json, html, text/plain",204 or an array `["json", "html", "text/plain"]`. When a list205 or array is given the _best_ match, if any is returned.206 207 ## Examples208 209 // Accept: text/html210 req.accepts('html');211 // => "html"212 213 // Accept: text/*, application/json214 req.accepts('html');215 // => "html"216 req.accepts('text/html');217 // => "text/html"218 req.accepts('json, text');219 // => "json"220 req.accepts('application/json');221 // => "application/json"222 223 // Accept: text/*, application/json224 req.accepts('image/png');225 req.accepts('png');226 // => undefined227 228 // Accept: text/*;q=.5, application/json229 req.accepts(['html', 'json']);230 req.accepts('html, json');231 // => "json"232233# req.acceptsCharset()234235 Check if the given `charset` is acceptable,236 otherwise you should respond with 406 "Not Acceptable".237238# req.acceptsLanguage()239240 Check if the given `lang` is acceptable,241 otherwise you should respond with 406 "Not Acceptable".242243# req.param()244245 Return the value of param `name` when present or `defaultValue`.246 247 - Checks body params, ex: id=12, {"id":12}248 - Checks route placeholders, ex: _/user/:id_249 - Checks query string params, ex: ?id=12250 251 To utilize request bodies, `req.body`252 should be an object. This can be done by using253 the `connect.bodyParser()` middleware.254255# req.is()256257 Check if the incoming request contains the "Content-Type" 258 header field, and it contains the give mime `type`.259 260 ## Examples261 262 // With Content-Type: text/html; charset=utf-8263 req.is('html');264 req.is('text/html');265 req.is('text/*');266 // => true267 268 // When Content-Type is application/json269 req.is('json');270 req.is('application/json');271 req.is('application/*');272 // => true273 274 req.is('html');275 // => false276 277 Now within our route callbacks, we can use to to assert content types278 such as "image/jpeg", "image/png", etc.279 280 app.post('/image/upload', function(req, res, next){281 if (req.is('image/*')) {282 // do something283 } else {284 next();285 }286 });287288289# res290291 Response prototype.292293# res.status()294295 Set status `code`.296297# res.send()298299 Send a response.300 301 ## Examples302 303 res.send(new Buffer('wahoo'));304 res.send({ some: 'json' });305 res.send('<p>some html</p>');306 res.send(404, 'Sorry, cant find that');307 res.send(404);308309# res.json()310311 Send JSON response.312 313 ## Examples314 315 res.json(null);316 res.json({ user: 'tj' });317 res.json(500, 'oh noes!');318 res.json(404, 'I dont have that');319320# res.sendfile()321322 Transfer the file at the given `path`.323 324 Automatically sets the _Content-Type_ response header field.325 The callback `fn(err)` is invoked when the transfer is complete326 or when an error occurs. Be sure to check `res.sentHeader`327 if you wish to attempt responding, as the header and some data328 may have already been transferred.329 330 ## Options331 332 - `maxAge` defaulting to 0333 - `root` root directory for relative filenames334 335 ## Examples336 337 The following example illustrates how `res.sendfile()` may338 be used as an alternative for the `static()` middleware for339 dynamic situations. The code backing `res.sendfile()` is actually340 the same code, so HTTP cache support etc is identical.341 342 app.get('/user/:uid/photos/:file', function(req, res){343 var uid = req.params.uid344 , file = req.params.file;345 346 req.user.mayViewFilesFrom(uid, function(yes){347 if (yes) {348 res.sendfile('/uploads/' + uid + '/' + file);349 } else {350 res.send(403, 'Sorry! you cant see that.');351 }352 });353 });354355# res.download()356357 Transfer the file at the given `path` as an attachment.358 359 Optionally providing an alternate attachment `filename`,360 and optional callback `fn(err)`. The callback is invoked361 when the data transfer is complete, or when an error has362 ocurred. Be sure to check `res.headerSent` if you plan to respond.363364# res.format()365366 Respond to the Acceptable formats using an `obj`367 of mime-type callbacks.368 369 This method uses `req.accepted`, an array of370 acceptable types ordered by their quality values.371 When "Accept" is not present the _first_ callback372 is invoked, otherwise the first match is used. When373 no match is performed the server responds with374 406 "Not Acceptable".375 376 Content-Type is set for you, however if you choose377 you may alter this within the callback using `res.type()`378 or `res.set('Content-Type', ...)`.379 380 res.format({381 'text/plain': function(){382 res.send('hey');383 },384 385 'text/html': function(){386 res.send('<p>hey</p>');387 },388 389 'appliation/json': function(){390 res.send({ message: 'hey' });391 }392 });393 394 In addition to canonicalized MIME types you may395 ## also use extnames mapped to these types396 397 res.format({398 text: function(){399 res.send('hey');400 },401 402 html: function(){403 res.send('<p>hey</p>');404 },405 406 json: function(){407 res.send({ message: 'hey' });408 }409 });410411# res.attachment()412413 Set _Content-Disposition_ header to _attachment_ with optional `filename`.414415# res.set()416417 Set header `field` to `val`, or pass418 an object of of header fields.419 420 ## Examples421 422 res.set('Accept', 'application/json');423 res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });424425# res.get()426427 Get value for header `field`.428429# res.clearCookie()430431 Clear cookie `name`.432433# res.signedCookie()434435 Set a signed cookie with the given `name` and `val`.436 See `res.cookie()` for details.437438# res.cookie()439440 Set cookie `name` to `val`, with the given `options`.441 442 ## Options443 444 - `maxAge` max-age in milliseconds, converted to `expires`445 - `path` defaults to "/"446 447 ## Examples448 449 // "Remember Me" for 15 minutes450 res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });451 452 // save as above453 res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })454455# res.redirect()456457 Redirect to the given `url` with optional response `status`458 defaulting to 302.459 460 The given `url` can also be the name of a mapped url, for461 example by default express supports "back" which redirects462 to the _Referrer_ or _Referer_ headers or "/".463 464 ## Examples465 466 res.redirect('/foo/bar');467 res.redirect('http://example.com');468 res.redirect(301, 'http://example.com');469 res.redirect('../login'); // /blog/post/1 -> /blog/login470 471 ## Mounting472 473 When an application is mounted, and `res.redirect()`474 is given a path that does _not_ lead with "/". For 475 example suppose a "blog" app is mounted at "/blog",476 the following redirect would result in "/blog/login":477 478 res.redirect('login');479 480 While the leading slash would result in a redirect to "/login":481 482 res.redirect('/login');483484# res.render()485486 Render `view` with the given `options` and optional callback `fn`.487 When a callback function is given a response will _not_ be made488 automatically, otherwise a response of _200_ and _text/html_ is given.489 490 ## Options491 492 - `status` Response status code (`res.statusCode`)493 - `charset` Set the charset (`res.charset`)494 495 ## Reserved locals496 497 - `cache` boolean hinting to the engine it should cache498 - `filename` filename of the view being rendered499
modified:Makefile+4 added-1 removed
12REPORTER = dot34docs: docs/application.md docs/request.md docs/response.md4docs: docs/express.md56docs/express.md: docs/application.md docs/request.md docs/response.md7 cat $^ > $@89docs/%.md: lib/%.js10 @mkdir -p docs11 dox --raw < $< | ./support/docs > $@12
19 unmodified lines