mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-21 19:41:33 +00:00
Initial Spanish language
This commit is contained in:
16
_includes/announcement/announcement-es.md
Normal file
16
_includes/announcement/announcement-es.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<ul>
|
||||
<li>
|
||||
<p><time datetime="2015-05-15 19:00">May 5, 2015</time> Ut enim ad minim veniam</p>
|
||||
<p>
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<p><time datetime="2015-05-15 19:00">May 5, 2015</time> Excepteur sint occaecat cupidatat non proident</p>
|
||||
<p>
|
||||
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
</p>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
57
_includes/api/es/3x/app-VERB.md
Normal file
57
_includes/api/es/3x/app-VERB.md
Normal file
@@ -0,0 +1,57 @@
|
||||
<h3 id='app.VERB'>app.VERB(path, [callback...], callback)</h3>
|
||||
|
||||
The `app.VERB()` methods provide the routing functionality
|
||||
in Express, where <strong>VERB</strong> is one of the HTTP verbs, such
|
||||
as `app.post()`. Multiple callbacks may be given, all are treated
|
||||
equally, and behave just like middleware, with the one exception that
|
||||
these callbacks may invoke `next('route')` to bypass the
|
||||
remaining route callback(s). This mechanism can be used to perform pre-conditions
|
||||
on a route then pass control to subsequent routes when there is no reason to proceed
|
||||
with the route matched.
|
||||
|
||||
The following snippet illustrates the most simple route definition possible. Express
|
||||
translates the path strings to regular expressions, used internally to match incoming requests.
|
||||
Query strings are <em>not</em> considered when peforming these matches, for example "GET /"
|
||||
would match the following route, as would "GET /?name=tobi".
|
||||
|
||||
~~~js
|
||||
app.get('/', function(req, res){
|
||||
res.send('hello world');
|
||||
});
|
||||
~~~
|
||||
|
||||
Regular expressions may also be used, and can be useful
|
||||
if you have very specific restraints, for example the following
|
||||
would match "GET /commits/71dbb9c" as well as "GET /commits/71dbb9c..4c084f9".
|
||||
|
||||
~~~js
|
||||
app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){
|
||||
var from = req.params[0];
|
||||
var to = req.params[1] || 'HEAD';
|
||||
res.send('commit range ' + from + '..' + to);
|
||||
});
|
||||
~~~
|
||||
|
||||
Several callbacks may also be passed, useful for re-using middleware
|
||||
that load resources, perform validations, etc.
|
||||
|
||||
~~~js
|
||||
app.get('/user/:id', user.load, function(){
|
||||
// ...
|
||||
})
|
||||
~~~
|
||||
|
||||
These callbacks may be passed within arrays as well, these arrays are
|
||||
simply flattened when passed:
|
||||
|
||||
~~~js
|
||||
var middleware = [loadForum, loadThread];
|
||||
|
||||
app.get('/forum/:fid/thread/:tid', middleware, function(){
|
||||
// ...
|
||||
})
|
||||
|
||||
app.post('/forum/:fid/thread/:tid', middleware, function(){
|
||||
// ...
|
||||
})
|
||||
~~~
|
||||
32
_includes/api/es/3x/app-all.md
Normal file
32
_includes/api/es/3x/app-all.md
Normal file
@@ -0,0 +1,32 @@
|
||||
<h3 id='app.all'>app.all(path, [callback...], callback)</h3>
|
||||
|
||||
This method functions just like the `app.VERB()` methods,
|
||||
however it matches all HTTP verbs.
|
||||
|
||||
This method is extremely useful for
|
||||
mapping "global" logic for specific path prefixes or arbitrary matches.
|
||||
For example if you placed the following route at the top of all other
|
||||
route definitions, it would require that all routes from that point on
|
||||
would require authentication, and automatically load a user. Keep in mind
|
||||
that these callbacks do not have to act as end points, `loadUser`
|
||||
can perform a task, then `next()` to continue matching subsequent
|
||||
routes.
|
||||
|
||||
~~~js
|
||||
app.all('*', requireAuthentication, loadUser);
|
||||
~~~
|
||||
|
||||
Or the equivalent:
|
||||
|
||||
~~~js
|
||||
app.all('*', requireAuthentication)
|
||||
app.all('*', loadUser);
|
||||
~~~
|
||||
|
||||
Another great example of this is white-listed "global" functionality. Here
|
||||
the example is much like before, however only restricting paths prefixed with
|
||||
"/api":
|
||||
|
||||
~~~js
|
||||
app.all('/api/*', requireAuthentication);
|
||||
~~~
|
||||
40
_includes/api/es/3x/app-configure.md
Normal file
40
_includes/api/es/3x/app-configure.md
Normal file
@@ -0,0 +1,40 @@
|
||||
<h3 id='app.configure'>app.configure([env], callback)</h3>
|
||||
|
||||
Conditionally invoke `callback` when `env` matches `app.get('env')`,
|
||||
aka `process.env.NODE_ENV`. This method remains for legacy reasons, and is effectively
|
||||
an `if` statement as illustrated in the following snippets. These functions are <em>not</em>
|
||||
required in order to use `app.set()` and other configuration methods.
|
||||
|
||||
~~~js
|
||||
// all environments
|
||||
app.configure(function(){
|
||||
app.set('title', 'My Application');
|
||||
})
|
||||
|
||||
// development only
|
||||
app.configure('development', function(){
|
||||
app.set('db uri', 'localhost/dev');
|
||||
})
|
||||
|
||||
// production only
|
||||
app.configure('production', function(){
|
||||
app.set('db uri', 'n.n.n.n/prod');
|
||||
})
|
||||
~~~
|
||||
|
||||
Is effectively sugar for:
|
||||
|
||||
~~~js
|
||||
// all environments
|
||||
app.set('title', 'My Application');
|
||||
|
||||
// development only
|
||||
if ('development' == app.get('env')) {
|
||||
app.set('db uri', 'localhost/dev');
|
||||
}
|
||||
|
||||
// production only
|
||||
if ('production' == app.get('env')) {
|
||||
app.set('db uri', 'n.n.n.n/prod');
|
||||
}
|
||||
~~~
|
||||
9
_includes/api/es/3x/app-disable.md
Normal file
9
_includes/api/es/3x/app-disable.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='app.disable'>app.disable(name)</h3>
|
||||
|
||||
Set setting `name` to `false`.
|
||||
|
||||
~~~js
|
||||
app.disable('trust proxy');
|
||||
app.get('trust proxy');
|
||||
// => false
|
||||
~~~
|
||||
12
_includes/api/es/3x/app-disabled.md
Normal file
12
_includes/api/es/3x/app-disabled.md
Normal file
@@ -0,0 +1,12 @@
|
||||
<h3 id='app.disabled'>app.disabled(name)</h3>
|
||||
|
||||
Check if setting `name` is disabled.
|
||||
|
||||
~~~js
|
||||
app.disabled('trust proxy');
|
||||
// => true
|
||||
|
||||
app.enable('trust proxy');
|
||||
app.disabled('trust proxy');
|
||||
// => false
|
||||
~~~
|
||||
9
_includes/api/es/3x/app-enable.md
Normal file
9
_includes/api/es/3x/app-enable.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='app.enable'>app.enable(name)</h3>
|
||||
|
||||
Set setting `name` to `true`.
|
||||
|
||||
~~~js
|
||||
app.enable('trust proxy');
|
||||
app.get('trust proxy');
|
||||
// => true
|
||||
~~~
|
||||
12
_includes/api/es/3x/app-enabled.md
Normal file
12
_includes/api/es/3x/app-enabled.md
Normal file
@@ -0,0 +1,12 @@
|
||||
<h3 id='app.enabled'>app.enabled(name)</h3>
|
||||
|
||||
Check if setting `name` is enabled.
|
||||
|
||||
~~~js
|
||||
app.enabled('trust proxy');
|
||||
// => false
|
||||
|
||||
app.enable('trust proxy');
|
||||
app.enabled('trust proxy');
|
||||
// => true
|
||||
~~~
|
||||
39
_includes/api/es/3x/app-engine.md
Normal file
39
_includes/api/es/3x/app-engine.md
Normal file
@@ -0,0 +1,39 @@
|
||||
<h3 id='app.engine'>app.engine(ext, callback)</h3>
|
||||
|
||||
Register the given template engine `callback` as `ext`
|
||||
|
||||
By default will `require()` the engine based on the
|
||||
file extension. For example if you try to render
|
||||
a "foo.jade" file Express will invoke the following internally,
|
||||
and cache the `require()` on subsequent calls to increase
|
||||
performance.
|
||||
|
||||
~~~js
|
||||
app.engine('jade', require('jade').__express);
|
||||
~~~
|
||||
|
||||
For engines that do not provide `.__express` out of the box -
|
||||
or if you wish to "map" a different extension to the template engine
|
||||
you may use this method. For example mapping the EJS template engine to
|
||||
".html" files:
|
||||
|
||||
~~~js
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
~~~
|
||||
|
||||
In this case EJS provides a `.renderFile()` method with
|
||||
the same signature that Express expects: `(path, options, callback)`,
|
||||
though note that it aliases this method as `ejs.__express` internally
|
||||
so if you're using ".ejs" extensions you dont need to do anything.
|
||||
|
||||
Some template engines do not follow this convention, the
|
||||
<a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
|
||||
library was created to map all of node's popular template
|
||||
engines to follow this convention, thus allowing them to
|
||||
work seemlessly within Express.
|
||||
|
||||
~~~js
|
||||
var engines = require('consolidate');
|
||||
app.engine('haml', engines.haml);
|
||||
app.engine('html', engines.hogan);
|
||||
~~~
|
||||
12
_includes/api/es/3x/app-get.md
Normal file
12
_includes/api/es/3x/app-get.md
Normal file
@@ -0,0 +1,12 @@
|
||||
<h3 id='app.get'>app.get(name)</h3>
|
||||
|
||||
Get setting `name` value.
|
||||
|
||||
~~~js
|
||||
app.get('title');
|
||||
// => undefined
|
||||
|
||||
app.set('title', 'My Site');
|
||||
app.get('title');
|
||||
// => "My Site"
|
||||
~~~
|
||||
36
_includes/api/es/3x/app-listen.md
Normal file
36
_includes/api/es/3x/app-listen.md
Normal file
@@ -0,0 +1,36 @@
|
||||
<h3 id='app.listen'>app.listen()</h3>
|
||||
|
||||
Bind and listen for connections on the given host and port,
|
||||
this method is identical to node's <a href="http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback">http.Server#listen()</a>.
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
app.listen(3000);
|
||||
~~~
|
||||
|
||||
The `app` returned by `express()` is in fact a JavaScript
|
||||
`Function`, designed to be passed to node's http servers as a callback
|
||||
to handle requests. This allows you to provide both HTTP and HTTPS versions of
|
||||
your app with the same codebase easily, as the app does not inherit from these,
|
||||
it is simply a callback:
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var https = require('https');
|
||||
var http = require('http');
|
||||
var app = express();
|
||||
|
||||
http.createServer(app).listen(80);
|
||||
https.createServer(options, app).listen(443);
|
||||
~~~
|
||||
|
||||
The `app.listen()` method is simply a convenience method defined as,
|
||||
if you wish to use HTTPS or provide both, use the technique above.
|
||||
|
||||
~~~js
|
||||
app.listen = function(){
|
||||
var server = http.createServer(this);
|
||||
return server.listen.apply(server, arguments);
|
||||
};
|
||||
~~~
|
||||
47
_includes/api/es/3x/app-locals.md
Normal file
47
_includes/api/es/3x/app-locals.md
Normal file
@@ -0,0 +1,47 @@
|
||||
<h3 id='app.locals'>app.locals</h3>
|
||||
|
||||
Application local variables are provided to all templates
|
||||
rendered within the application. This is useful for providing
|
||||
helper functions to templates, as well as app-level data.
|
||||
|
||||
~~~js
|
||||
app.locals.title = 'My App';
|
||||
app.locals.strftime = require('strftime');
|
||||
~~~
|
||||
|
||||
The `app.locals` object is a JavaScript `Function`,
|
||||
which when invoked with an object will merge properties into itself, providing
|
||||
a simple way to expose existing objects as local variables.
|
||||
|
||||
~~~js
|
||||
app.locals({
|
||||
title: 'My App',
|
||||
phone: '1-250-858-9990',
|
||||
email: 'me@myapp.com'
|
||||
});
|
||||
|
||||
app.locals.title
|
||||
// => 'My App'
|
||||
|
||||
app.locals.email
|
||||
// => 'me@myapp.com'
|
||||
~~~
|
||||
|
||||
A consequence of the `app.locals` Object being ultimately a Javascript Function Object is that you must not reuse existing (native) named properties for your own variable names, such as `name, apply, bind, call, arguments, length, constructor`.
|
||||
|
||||
~~~js
|
||||
app.locals({name: 'My App'});
|
||||
|
||||
app.locals.name
|
||||
// => return 'app.locals' in place of 'My App' (app.locals is a Function !)
|
||||
// => if name's variable is used in a template, a ReferenceError will be returned.
|
||||
~~~
|
||||
|
||||
The full list of native named properties can be found in many specifications. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference">JavaScript specification</a> introduced original properties, some of which still recognized by modern engines, and the <a href="http://www.ecma-international.org/ecma-262/5.1/">EcmaScript specification</a> then built on it and normalized the set of properties, adding new ones and removing deprecated ones. Check out properties for Functions and Objects if interested.
|
||||
|
||||
By default Express exposes only a single app-level local variable, `settings`.
|
||||
|
||||
~~~js
|
||||
app.set('title', 'My App');
|
||||
// use settings.title in a view
|
||||
~~~
|
||||
70
_includes/api/es/3x/app-param.md
Normal file
70
_includes/api/es/3x/app-param.md
Normal file
@@ -0,0 +1,70 @@
|
||||
<h3 id='app.param'>app.param([name], callback)</h3>
|
||||
|
||||
Map logic to route parameters. For example when `:user`
|
||||
is present in a route path you may map user loading logic to automatically
|
||||
provide `req.user` to the route, or perform validations
|
||||
on the parameter input.
|
||||
|
||||
The following snippet illustrates how the `callback`
|
||||
is much like middleware, thus supporting async operations, however
|
||||
providing the additional value of the parameter, here named as `id`.
|
||||
An attempt to load the user is then performed, assigning `req.user`,
|
||||
otherwise passing an error to `next(err)`.
|
||||
|
||||
~~~js
|
||||
app.param('user', function(req, res, next, id){
|
||||
User.find(id, function(err, user){
|
||||
if (err) {
|
||||
next(err);
|
||||
} else if (user) {
|
||||
req.user = user;
|
||||
next();
|
||||
} else {
|
||||
next(new Error('failed to load user'));
|
||||
}
|
||||
});
|
||||
});
|
||||
~~~
|
||||
|
||||
Alternatively you may pass only a `callback`, in which
|
||||
case you have the opportunity to alter the `app.param()` API.
|
||||
For example the <a href="http://github.com/expressjs/express-params">express-params</a>
|
||||
defines the following callback which allows you to restrict parameters to a given
|
||||
regular expression.
|
||||
|
||||
This example is a bit more advanced, checking if the second argument is a regular
|
||||
expression, returning the callback which acts much like the "user" param example.
|
||||
|
||||
~~~js
|
||||
app.param(function(name, fn){
|
||||
if (fn instanceof RegExp) {
|
||||
return function(req, res, next, val){
|
||||
var captures;
|
||||
if (captures = fn.exec(String(val))) {
|
||||
req.params[name] = captures;
|
||||
next();
|
||||
} else {
|
||||
next('route');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
~~~
|
||||
|
||||
The method could now be used to effectively validate parameters, or also
|
||||
parse them to provide capture groups:
|
||||
|
||||
~~~js
|
||||
app.param('id', /^\d+$/);
|
||||
|
||||
app.get('/user/:id', function(req, res){
|
||||
res.send('user ' + req.params.id);
|
||||
});
|
||||
|
||||
app.param('range', /^(\w+)\.\.(\w+)?$/);
|
||||
|
||||
app.get('/range/:range', function(req, res){
|
||||
var range = req.params.range;
|
||||
res.send('from ' + range[1] + ' to ' + range[2]);
|
||||
});
|
||||
~~~
|
||||
15
_includes/api/es/3x/app-render.md
Normal file
15
_includes/api/es/3x/app-render.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='app.render'>app.render(view, [options], callback)</h3>
|
||||
|
||||
Render a `view` with a callback responding with
|
||||
the rendered string. This is the app-level variant of `res.render()`,
|
||||
and otherwise behaves the same way.
|
||||
|
||||
~~~js
|
||||
app.render('email', function(err, html){
|
||||
// ...
|
||||
});
|
||||
|
||||
app.render('email', { name: 'Tobi' }, function(err, html){
|
||||
// ...
|
||||
});
|
||||
~~~
|
||||
29
_includes/api/es/3x/app-routes.md
Normal file
29
_includes/api/es/3x/app-routes.md
Normal file
@@ -0,0 +1,29 @@
|
||||
<h3 id='app.routes'>app.routes</h3>
|
||||
|
||||
The `app.routes` object houses all of the routes defined mapped
|
||||
by the associated HTTP verb. This object may be used for introspection capabilities,
|
||||
for example Express uses this internally not only for routing but to provide default
|
||||
<string>OPTIONS</string> behaviour unless `app.options()` is used. Your application
|
||||
or framework may also remove routes by simply by removing them from this object.
|
||||
|
||||
~~~js
|
||||
console.log(app.routes)
|
||||
|
||||
{ get:
|
||||
[ { path: '/',
|
||||
method: 'get',
|
||||
callbacks: [Object],
|
||||
keys: [],
|
||||
regexp: /^\/\/?$/i },
|
||||
{ path: '/user/:id',
|
||||
method: 'get',
|
||||
callbacks: [Object],
|
||||
keys: [{ name: 'id', optional: false }],
|
||||
regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ],
|
||||
delete:
|
||||
[ { path: '/user/:id',
|
||||
method: 'delete',
|
||||
callbacks: [Object],
|
||||
keys: [Object],
|
||||
regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ] }
|
||||
~~~
|
||||
9
_includes/api/es/3x/app-set.md
Normal file
9
_includes/api/es/3x/app-set.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='app.set'>app.set(name, value)</h3>
|
||||
|
||||
Assigns setting `name` to `value`.
|
||||
|
||||
~~~js
|
||||
app.set('title', 'My Site');
|
||||
app.get('title');
|
||||
// => "My Site"
|
||||
~~~
|
||||
15
_includes/api/es/3x/app-settings.md
Normal file
15
_includes/api/es/3x/app-settings.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='app-settings'>settings</h3>
|
||||
|
||||
The following settings are provided to alter how Express will behave:
|
||||
|
||||
* `env` Environment mode, defaults to process.env.NODE_ENV or "development"
|
||||
* `trust proxy` Enables reverse proxy support, disabled by default
|
||||
* `jsonp callback name` Changes the default callback name of ?callback=
|
||||
* `json replacer` JSON replacer callback, null by default
|
||||
* `json spaces` JSON response spaces for formatting, defaults to 2 in development, 0 in production
|
||||
* `case sensitive routing` Enable case sensitivity, disabled by default, treating "/Foo" and "/foo" as the same
|
||||
* `strict routing` Enable strict routing, by default "/foo" and "/foo/" are treated the same by the router
|
||||
* `view cache` Enables view template compilation caching, enabled in production by default
|
||||
* `view engine` The default engine extension to use when omitted
|
||||
* `views` The view directory path, defaulting to "process.cwd() + '/views'"
|
||||
|
||||
89
_includes/api/es/3x/app-use.md
Normal file
89
_includes/api/es/3x/app-use.md
Normal file
@@ -0,0 +1,89 @@
|
||||
<h3 id='app.use'>app.use([path], function)</h3>
|
||||
|
||||
Use the given middleware `function`, with optional mount `path`,
|
||||
defaulting to "/".
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
|
||||
// simple logger
|
||||
app.use(function(req, res, next){
|
||||
console.log('%s %s', req.method, req.url);
|
||||
next();
|
||||
});
|
||||
|
||||
// respond
|
||||
app.use(function(req, res, next){
|
||||
res.send('Hello World');
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
~~~
|
||||
|
||||
The "mount" path is stripped and is <strong>not</strong> visible
|
||||
to the middleware `function`. The main effect of this feature is that
|
||||
mounted middleware may operate without code changes regardless of its "prefix"
|
||||
pathname.
|
||||
|
||||
<div class="doc-box doc-notice" markdown="1">
|
||||
A route will match any path, which follows its path immediately with either a "`/`" or a "`.`". For example: `app.use('/apple', ...)` will match _/apple_, _/apple/images_, _/apple/images/news_, _/apple.html_, _/apple.html.txt_, and so on.
|
||||
</div>
|
||||
|
||||
Here's a concrete example, take the typical use-case of serving files in ./public
|
||||
using the `express.static()` middleware:
|
||||
|
||||
~~~js
|
||||
// GET /javascripts/jquery.js
|
||||
// GET /style.css
|
||||
// GET /favicon.ico
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
~~~
|
||||
|
||||
Say for example you wanted to prefix all static files with "/static", you could
|
||||
use the "mounting" feature to support this. Mounted middleware functions are _not_
|
||||
invoked unless the `req.url` contains this prefix, at which point
|
||||
it is stripped when the function is invoked. This affects this function only,
|
||||
subsequent middleware will see `req.url` with "/static" included
|
||||
unless they are mounted as well.
|
||||
|
||||
~~~js
|
||||
// GET /static/javascripts/jquery.js
|
||||
// GET /static/style.css
|
||||
// GET /static/favicon.ico
|
||||
app.use('/static', express.static(__dirname + '/public'));
|
||||
~~~
|
||||
|
||||
The order of which middleware are "defined" using `app.use()` is
|
||||
very important, they are invoked sequentially, thus this defines middleware
|
||||
precedence. For example usually `express.logger()` is the very
|
||||
first middleware you would use, logging every request:
|
||||
|
||||
~~~js
|
||||
app.use(express.logger());
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(function(req, res){
|
||||
res.send('Hello');
|
||||
});
|
||||
~~~
|
||||
|
||||
Now suppose you wanted to ignore logging requests for static files, but to
|
||||
continue logging routes and middleware defined after `logger()`,
|
||||
you would simply move `static()` above:
|
||||
|
||||
~~~js
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(express.logger());
|
||||
app.use(function(req, res){
|
||||
res.send('Hello');
|
||||
});
|
||||
~~~
|
||||
|
||||
Another concrete example would be serving files from multiple directories,
|
||||
giving precedence to "./public" over the others:
|
||||
|
||||
~~~js
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(express.static(__dirname + '/files'));
|
||||
app.use(express.static(__dirname + '/uploads'));
|
||||
~~~
|
||||
69
_includes/api/es/3x/app.md
Normal file
69
_includes/api/es/3x/app.md
Normal file
@@ -0,0 +1,69 @@
|
||||
<h2>Application</h2>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-set.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-get.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-enable.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-disable.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-enabled.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-disabled.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-configure.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-use.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-settings.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-engine.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-param.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-VERB.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-all.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-locals.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-render.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-routes.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/app-listen.md %}
|
||||
</section>
|
||||
14
_includes/api/es/3x/express.md
Normal file
14
_includes/api/es/3x/express.md
Normal file
@@ -0,0 +1,14 @@
|
||||
<h2>express()</h2>
|
||||
|
||||
Creates an Express application. The `express()` function is a top-level function exported by the _express_ module.
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send('hello world');
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
~~~
|
||||
159
_includes/api/es/3x/menu.md
Normal file
159
_includes/api/es/3x/menu.md
Normal file
@@ -0,0 +1,159 @@
|
||||
<ul id="menu">
|
||||
<li><a href="#express">express()</a></li>
|
||||
<li id="app-api"> <a href="#application">Application</a>
|
||||
<ul id="app-menu">
|
||||
<li><a href="#app.set">app.set()</a>
|
||||
</li>
|
||||
<li><a href="#app.get">app.get()</a>
|
||||
</li>
|
||||
<li><a href="#app.enable">app.enable()</a>
|
||||
</li>
|
||||
<li><a href="#app.disable">app.disable()</a>
|
||||
</li>
|
||||
<li><a href="#app.enabled">app.enabled()</a>
|
||||
</li>
|
||||
<li><a href="#app.disabled">app.disabled()</a>
|
||||
</li>
|
||||
<li><a href="#app.configure">app.configure()</a>
|
||||
</li>
|
||||
<li><a href="#app.use">app.use()</a>
|
||||
</li>
|
||||
<li><a href="#app-settings">application settings</a>
|
||||
</li>
|
||||
<li><a href="#app.engine">app.engine()</a>
|
||||
</li>
|
||||
<li><a href="#app.param">app.param()</a>
|
||||
</li>
|
||||
<li><a href="#app.VERB">application routing</a>
|
||||
</li>
|
||||
<li><a href="#app.all">app.all()</a>
|
||||
</li>
|
||||
<li><a href="#app.locals">app.locals</a>
|
||||
</li>
|
||||
<li><a href="#app.render">app.render()</a>
|
||||
</li>
|
||||
<li><a href="#app.routes">app.routes</a>
|
||||
</li>
|
||||
<li><a href="#app.listen">app.listen()</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li id="req-api"> <a href="#request">Request</a>
|
||||
<ul id="req-menu">
|
||||
<li><a href="#req.params">req.params</a>
|
||||
</li>
|
||||
<li><a href="#req.query">req.query</a>
|
||||
</li>
|
||||
<li><a href="#req.body">req.body</a>
|
||||
</li>
|
||||
<li><a href="#req.files">req.files</a>
|
||||
</li>
|
||||
<li><a href="#req.param">req.param()</a>
|
||||
</li>
|
||||
<li><a href="#req.route">req.route</a>
|
||||
</li>
|
||||
<li><a href="#req.cookies">req.cookies</a>
|
||||
</li>
|
||||
<li><a href="#req.signedCookies">req.signedCookies</a>
|
||||
</li>
|
||||
<li><a href="#req.get">req.get()</a>
|
||||
</li>
|
||||
<li><a href="#req.accepts">req.accepts()</a>
|
||||
</li>
|
||||
<li><a href="#req.accepted">req.accepted</a>
|
||||
</li>
|
||||
<li><a href="#req.is">req.is()</a>
|
||||
</li>
|
||||
<li><a href="#req.ip">req.ip</a>
|
||||
</li>
|
||||
<li><a href="#req.ips">req.ips</a>
|
||||
</li>
|
||||
<li><a href="#req.path">req.path</a>
|
||||
</li>
|
||||
<li><a href="#req.host">req.host</a>
|
||||
</li>
|
||||
<li><a href="#req.fresh">req.fresh</a>
|
||||
</li>
|
||||
<li><a href="#req.stale">req.stale</a>
|
||||
</li>
|
||||
<li><a href="#req.xhr">req.xhr</a>
|
||||
</li>
|
||||
<li><a href="#req.protocol">req.protocol</a>
|
||||
</li>
|
||||
<li><a href="#req.secure">req.secure</a>
|
||||
</li>
|
||||
<li><a href="#req.subdomains">req.subdomains</a>
|
||||
</li>
|
||||
<li><a href="#req.originalUrl">req.originalUrl</a>
|
||||
</li>
|
||||
<li><a href="#req.acceptedLanguages">req.acceptedLanguages</a>
|
||||
</li>
|
||||
<li><a href="#req.acceptedCharsets">req.acceptedCharsets</a>
|
||||
</li>
|
||||
<li><a href="#req.acceptsCharset">req.acceptsCharset()</a>
|
||||
</li>
|
||||
<li><a href="#req.acceptsLanguage">req.acceptsLanguage()</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li id="res-api"> <a href="#response">Response</a>
|
||||
<ul id="res-menu">
|
||||
<li><a href="#res.status">res.status()</a>
|
||||
</li>
|
||||
<li><a href="#res.set">res.set()</a>
|
||||
</li>
|
||||
<li><a href="#res.get">res.get()</a>
|
||||
</li>
|
||||
<li><a href="#res.cookie">res.cookie()</a>
|
||||
</li>
|
||||
<li><a href="#res.clearCookie">res.clearCookie()</a>
|
||||
</li>
|
||||
<li><a href="#res.redirect">res.redirect()</a>
|
||||
</li>
|
||||
<li><a href="#res.location">res.location()</a>
|
||||
</li>
|
||||
<li><a href="#res.charset">res.charset</a>
|
||||
</li>
|
||||
<li><a href="#res.send">res.send()</a>
|
||||
</li>
|
||||
<li><a href="#res.json">res.json()</a>
|
||||
</li>
|
||||
<li><a href="#res.jsonp">res.jsonp()</a>
|
||||
</li>
|
||||
<li><a href="#res.type">res.type()</a>
|
||||
</li>
|
||||
<li><a href="#res.format">res.format()</a>
|
||||
</li>
|
||||
<li><a href="#res.attachment">res.attachment()</a>
|
||||
</li>
|
||||
<li><a href="#res.sendfile">res.sendfile()</a>
|
||||
</li>
|
||||
<li><a href="#res.download">res.download()</a>
|
||||
</li>
|
||||
<li><a href="#res.links">res.links()</a>
|
||||
</li>
|
||||
<li><a href="#res.locals">res.locals</a>
|
||||
</li>
|
||||
<li><a href="#res.render">res.render()</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li id="middleware-api"><a href="#middleware">Middleware</a>
|
||||
<ul id="middleware-menu">
|
||||
<li><a href="#basicAuth">basicAuth()</a>
|
||||
</li>
|
||||
<li><a href="#bodyParser">bodyParser()</a>
|
||||
</li>
|
||||
<li><a href="#compress">compress()</a>
|
||||
</li>
|
||||
<li><a href="#cookieParser">cookieParser()</a>
|
||||
</li>
|
||||
<li><a href="#cookieSession">cookieSession()</a>
|
||||
</li>
|
||||
<li><a href="#csrf">csrf()</a>
|
||||
</li>
|
||||
<li><a href="#directory">directory()</a>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
29
_includes/api/es/3x/middleware.md
Normal file
29
_includes/api/es/3x/middleware.md
Normal file
@@ -0,0 +1,29 @@
|
||||
<h2>Middleware</h2>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-basicAuth.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-bodyParser.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-compress.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-cookieParser.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-cookieSession.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-csrf.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/mw-directory.md %}
|
||||
</section>
|
||||
27
_includes/api/es/3x/mw-basicAuth.md
Normal file
27
_includes/api/es/3x/mw-basicAuth.md
Normal file
@@ -0,0 +1,27 @@
|
||||
<h3 id='basicAuth'>basicAuth()</h3>
|
||||
|
||||
Basic Authentication middleware, populating `req.user`
|
||||
with the username.
|
||||
|
||||
Simple username and password:
|
||||
|
||||
~~~js
|
||||
app.use(express.basicAuth('username', 'password'));
|
||||
~~~
|
||||
|
||||
Callback verification:
|
||||
|
||||
~~~js
|
||||
app.use(express.basicAuth(function(user, pass){
|
||||
return 'tj' == user && 'wahoo' == pass;
|
||||
}));
|
||||
~~~
|
||||
|
||||
Async callback verification, accepting `fn(err, user)`,
|
||||
in this case `req.user` will be the user object passed.
|
||||
|
||||
~~~js
|
||||
app.use(express.basicAuth(function(user, pass, fn){
|
||||
User.authenticate({ user: user, pass: pass }, fn);
|
||||
}))
|
||||
~~~
|
||||
27
_includes/api/es/3x/mw-bodyParser.md
Normal file
27
_includes/api/es/3x/mw-bodyParser.md
Normal file
@@ -0,0 +1,27 @@
|
||||
<h3 id='bodyParser'>bodyParser()</h3>
|
||||
|
||||
Request body parsing middleware supporting JSON, urlencoded,
|
||||
and multipart requests. This middleware is simply a wrapper
|
||||
for the `json()`, `urlencoded()`, and
|
||||
`multipart()` middleware.
|
||||
|
||||
~~~js
|
||||
app.use(express.bodyParser());
|
||||
|
||||
// is equivalent to:
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded());
|
||||
app.use(express.multipart());
|
||||
~~~
|
||||
|
||||
For security sake, it's better to disable file upload if your application
|
||||
doesn't need it. To do this, use only the needed middleware, i.e. don't use
|
||||
the `bodyParser` and `multipart()` middleware:
|
||||
|
||||
~~~js
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded());
|
||||
~~~
|
||||
|
||||
If your application needs file upload you should set up
|
||||
<a href='https://groups.google.com/d/msg/express-js/iP2VyhkypHo/5AXQiYN3RPcJ'>a strategy for dealing with those files</a>.
|
||||
12
_includes/api/es/3x/mw-compress.md
Normal file
12
_includes/api/es/3x/mw-compress.md
Normal file
@@ -0,0 +1,12 @@
|
||||
<h3 id='compress'>compress()</h3>
|
||||
|
||||
Compress response data with gzip / deflate. This middleware
|
||||
should be placed "high" within the stack to ensure all
|
||||
responses may be compressed.
|
||||
|
||||
~~~js
|
||||
app.use(express.logger());
|
||||
app.use(express.compress());
|
||||
app.use(express.methodOverride());
|
||||
app.use(express.bodyParser());
|
||||
~~~
|
||||
10
_includes/api/es/3x/mw-cookieParser.md
Normal file
10
_includes/api/es/3x/mw-cookieParser.md
Normal file
@@ -0,0 +1,10 @@
|
||||
<h3 id='cookieParser'>cookieParser()</h3>
|
||||
|
||||
Parses the Cookie header field and populates `req.cookies`
|
||||
with an object keyed by the cookie names. Optionally you may enabled
|
||||
signed cookie support by passing a `secret` string.
|
||||
|
||||
~~~js
|
||||
app.use(express.cookieParser());
|
||||
app.use(express.cookieParser('some secret'));
|
||||
~~~
|
||||
19
_includes/api/es/3x/mw-cookieSession.md
Normal file
19
_includes/api/es/3x/mw-cookieSession.md
Normal file
@@ -0,0 +1,19 @@
|
||||
<h3 id='cookieSession'>cookieSession()</h3>
|
||||
|
||||
Provides cookie-based sessions, and populates `req.session`.
|
||||
This middleware takes the following options:
|
||||
|
||||
* `key` cookie name defaulting to `connect.sess`
|
||||
* `secret` prevents cookie tampering
|
||||
* `cookie` session cookie settings, defaulting to `{ path: '/', httpOnly: true, maxAge: null }`
|
||||
* `proxy` trust the reverse proxy when setting secure cookies (via "x-forwarded-proto")
|
||||
|
||||
~~~js
|
||||
app.use(express.cookieSession());
|
||||
~~~
|
||||
|
||||
To clear a cookie simply assign the session to null before responding:
|
||||
|
||||
~~~js
|
||||
req.session = null
|
||||
~~~
|
||||
15
_includes/api/es/3x/mw-csrf.md
Normal file
15
_includes/api/es/3x/mw-csrf.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='csrf'>csrf()</h3>
|
||||
|
||||
CSRF protection middleware.
|
||||
|
||||
By default this middleware generates a token named "_csrf"
|
||||
which should be added to requests which mutate
|
||||
state, within a hidden form field, query-string etc. This
|
||||
token is validated against `req.csrfToken()`.
|
||||
|
||||
The default `value` function checks `req.body` generated
|
||||
by the `bodyParser()` middleware, `req.query` generated
|
||||
by `query()`, and the "X-CSRF-Token" header field.
|
||||
|
||||
This middleware requires session support, thus should be added
|
||||
somewhere below `session()`.
|
||||
16
_includes/api/es/3x/mw-directory.md
Normal file
16
_includes/api/es/3x/mw-directory.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='directory'>directory()</h3>
|
||||
|
||||
Directory serving middleware, serves the given `path`.
|
||||
This middleware may be paired with `static()` to serve
|
||||
files, providing a full-featured file browser.
|
||||
|
||||
~~~js
|
||||
app.use(express.directory('public'))
|
||||
app.use(express.static('public'))
|
||||
~~~
|
||||
|
||||
This middleware accepts the following options:
|
||||
|
||||
* `hidden` display hidden (dot) files. Defaults to false.
|
||||
* `icons` display icons. Defaults to false.
|
||||
* `filter` Apply this filter function to files. Defaults to false.
|
||||
14
_includes/api/es/3x/req-accepted.md
Normal file
14
_includes/api/es/3x/req-accepted.md
Normal file
@@ -0,0 +1,14 @@
|
||||
<h3 id='req.accepted'>req.accepted</h3>
|
||||
|
||||
Return an array of Accepted media types ordered from highest quality to lowest.
|
||||
|
||||
~~~js
|
||||
[ { value: 'application/json',
|
||||
quality: 1,
|
||||
type: 'application',
|
||||
subtype: 'json' },
|
||||
{ value: 'text/html',
|
||||
quality: 0.5,
|
||||
type: 'text',
|
||||
subtype: 'html' } ]
|
||||
~~~
|
||||
8
_includes/api/es/3x/req-acceptedCharsets.md
Normal file
8
_includes/api/es/3x/req-acceptedCharsets.md
Normal file
@@ -0,0 +1,8 @@
|
||||
<h3 id='req.acceptedCharsets'>req.acceptedCharsets</h3>
|
||||
|
||||
Return an array of Accepted charsets ordered from highest quality to lowest.
|
||||
|
||||
~~~js
|
||||
Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
|
||||
// => ['unicode-1-1', 'iso-8859-5']
|
||||
~~~
|
||||
8
_includes/api/es/3x/req-acceptedLanguages.md
Normal file
8
_includes/api/es/3x/req-acceptedLanguages.md
Normal file
@@ -0,0 +1,8 @@
|
||||
<h3 id='req.acceptedLanguages'>req.acceptedLanguages</h3>
|
||||
|
||||
Return an array of Accepted languages ordered from highest quality to lowest.
|
||||
|
||||
~~~js
|
||||
Accept-Language: en;q=.5, en-us
|
||||
// => ['en-us', 'en']
|
||||
~~~
|
||||
36
_includes/api/es/3x/req-accepts.md
Normal file
36
_includes/api/es/3x/req-accepts.md
Normal file
@@ -0,0 +1,36 @@
|
||||
<h3 id='req.accepts'>req.accepts(types)</h3>
|
||||
|
||||
Check if the given `types` are acceptable, returning
|
||||
the best match when true, otherwise `undefined` - in which
|
||||
case you should respond with 406 "Not Acceptable".
|
||||
|
||||
The `type` value may be a single mime type string
|
||||
such as "application/json", the extension name
|
||||
such as "json", a comma-delimited list or an array. When a list
|
||||
or array is given the best match, if any is returned.
|
||||
|
||||
~~~js
|
||||
// Accept: text/html
|
||||
req.accepts('html');
|
||||
// => "html"
|
||||
|
||||
// Accept: text/*, application/json
|
||||
req.accepts('html');
|
||||
// => "html"
|
||||
req.accepts('text/html');
|
||||
// => "text/html"
|
||||
req.accepts('json, text');
|
||||
// => "json"
|
||||
req.accepts('application/json');
|
||||
// => "application/json"
|
||||
|
||||
// Accept: text/*, application/json
|
||||
req.accepts('image/png');
|
||||
req.accepts('png');
|
||||
// => undefined
|
||||
|
||||
// Accept: text/*;q=.5, application/json
|
||||
req.accepts(['html', 'json']);
|
||||
req.accepts('html, json');
|
||||
// => "json"
|
||||
~~~
|
||||
3
_includes/api/es/3x/req-acceptsCharset.md
Normal file
3
_includes/api/es/3x/req-acceptsCharset.md
Normal file
@@ -0,0 +1,3 @@
|
||||
<h3 id='req.acceptsCharset'>req.acceptsCharset(charset)</h3>
|
||||
|
||||
Check if the given `charset` are acceptable.
|
||||
3
_includes/api/es/3x/req-acceptsLanguage.md
Normal file
3
_includes/api/es/3x/req-acceptsLanguage.md
Normal file
@@ -0,0 +1,3 @@
|
||||
<h3 id='req.acceptsLanguage'>req.acceptsLanguage(lang)</h3>
|
||||
|
||||
Check if the given `lang` are acceptable.
|
||||
19
_includes/api/es/3x/req-body.md
Normal file
19
_includes/api/es/3x/req-body.md
Normal file
@@ -0,0 +1,19 @@
|
||||
<h3 id='req.body'>req.body</h3>
|
||||
|
||||
This property is an object containing the parsed request body. This feature
|
||||
is provided by the `bodyParser()` middleware, though other body
|
||||
parsing middleware may follow this convention as well. This property
|
||||
defaults to `{}` when `bodyParser()` is used.
|
||||
|
||||
~~~js
|
||||
// POST user[name]=tobi&user[email]=tobi@learnboost.com
|
||||
req.body.user.name
|
||||
// => "tobi"
|
||||
|
||||
req.body.user.email
|
||||
// => "tobi@learnboost.com"
|
||||
|
||||
// POST { "name": "tobi" }
|
||||
req.body.name
|
||||
// => "tobi"
|
||||
~~~
|
||||
11
_includes/api/es/3x/req-cookies.md
Normal file
11
_includes/api/es/3x/req-cookies.md
Normal file
@@ -0,0 +1,11 @@
|
||||
<h3 id='req.cookies'>req.cookies</h3>
|
||||
|
||||
This object requires the `cookieParser()` middleware for use.
|
||||
It contains cookies sent by the user-agent. If no cookies are sent, it
|
||||
defaults to `{}`.
|
||||
|
||||
~~~js
|
||||
// Cookie: name=tj
|
||||
req.cookies.name
|
||||
// => "tj"
|
||||
~~~
|
||||
45
_includes/api/es/3x/req-files.md
Normal file
45
_includes/api/es/3x/req-files.md
Normal file
@@ -0,0 +1,45 @@
|
||||
<h3 id='req.files'>req.files</h3>
|
||||
|
||||
This property is an object of the files uploaded. This feature
|
||||
is provided by the `bodyParser()` middleware, though other body
|
||||
parsing middleware may follow this convention as well. This property
|
||||
defaults to `{}` when `bodyParser()` is used.
|
||||
|
||||
For example if a <strong>file</strong> field was named "image",
|
||||
and a file was uploaded, `req.files.image` would contain
|
||||
the following `File` object:
|
||||
|
||||
~~~js
|
||||
{ size: 74643,
|
||||
path: '/tmp/8ef9c52abe857867fd0a4e9a819d1876',
|
||||
name: 'edge.png',
|
||||
type: 'image/png',
|
||||
hash: false,
|
||||
lastModifiedDate: Thu Aug 09 2012 20:07:51 GMT-0700 (PDT),
|
||||
_writeStream:
|
||||
{ path: '/tmp/8ef9c52abe857867fd0a4e9a819d1876',
|
||||
fd: 13,
|
||||
writable: false,
|
||||
flags: 'w',
|
||||
encoding: 'binary',
|
||||
mode: 438,
|
||||
bytesWritten: 74643,
|
||||
busy: false,
|
||||
_queue: [],
|
||||
_open: [Function],
|
||||
drainable: true },
|
||||
length: [Getter],
|
||||
filename: [Getter],
|
||||
mime: [Getter] }
|
||||
~~~
|
||||
|
||||
The `bodyParser()` middleware utilizes the
|
||||
<a href="https://github.com/felixge/node-formidable">node-formidable</a>
|
||||
module internally, and accepts the same options. An example of this
|
||||
is the `keepExtensions` formidable option, defaulting to <strong>false</strong>
|
||||
which in this case gives you the filename "/tmp/8ef9c52abe857867fd0a4e9a819d1876" void of
|
||||
the ".png" extension. To enable this, and others you may pass them to `bodyParser()`:
|
||||
|
||||
~~~js
|
||||
app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-fresh.md
Normal file
9
_includes/api/es/3x/req-fresh.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.fresh'>req.fresh</h3>
|
||||
|
||||
Check if the request is fresh - aka Last-Modified and/or the ETag still match,
|
||||
indicating that the resource is "fresh".
|
||||
|
||||
~~~js
|
||||
req.fresh
|
||||
// => true
|
||||
~~~
|
||||
16
_includes/api/es/3x/req-header.md
Normal file
16
_includes/api/es/3x/req-header.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='req.get'>req.get(field)</h3>
|
||||
|
||||
Get the case-insensitive request header `field`. The "Referrer" and "Referer" fields are interchangeable.
|
||||
|
||||
~~~js
|
||||
req.get('Content-Type');
|
||||
// => "text/plain"
|
||||
|
||||
req.get('content-type');
|
||||
// => "text/plain"
|
||||
|
||||
req.get('Something');
|
||||
// => undefined
|
||||
~~~
|
||||
|
||||
p Aliased as `req.header(field)`.
|
||||
9
_includes/api/es/3x/req-host.md
Normal file
9
_includes/api/es/3x/req-host.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.host'>req.host</h3>
|
||||
|
||||
Returns the hostname from the "Host" header field (void of portno).
|
||||
|
||||
~~~js
|
||||
// Host: "example.com:3000"
|
||||
req.host
|
||||
// => "example.com"
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-ip.md
Normal file
9
_includes/api/es/3x/req-ip.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.ip'>req.ip</h3>
|
||||
|
||||
Return the remote address, or when "trust proxy"
|
||||
is enabled - the upstream address.
|
||||
|
||||
~~~js
|
||||
req.ip
|
||||
// => "127.0.0.1"
|
||||
~~~
|
||||
10
_includes/api/es/3x/req-ips.md
Normal file
10
_includes/api/es/3x/req-ips.md
Normal file
@@ -0,0 +1,10 @@
|
||||
<h3 id='req.ips'>req.ips</h3>
|
||||
|
||||
When "trust proxy" is `true`, parse
|
||||
the "X-Forwarded-For" ip address list
|
||||
and return an array, otherwise an empty
|
||||
array is returned.
|
||||
|
||||
For example if the value were "client, proxy1, proxy2"
|
||||
you would receive the array `["client", "proxy1", "proxy2"]`
|
||||
where "proxy2" is the furthest down-stream.
|
||||
21
_includes/api/es/3x/req-is.md
Normal file
21
_includes/api/es/3x/req-is.md
Normal file
@@ -0,0 +1,21 @@
|
||||
<h3 id='req.is'>req.is(type)</h3>
|
||||
|
||||
Check if the incoming request contains the "Content-Type"
|
||||
header field, and it matches the give mime `type`.
|
||||
|
||||
~~~js
|
||||
// With Content-Type: text/html; charset=utf-8
|
||||
req.is('html');
|
||||
req.is('text/html');
|
||||
req.is('text/*');
|
||||
// => true
|
||||
|
||||
// When Content-Type is application/json
|
||||
req.is('json');
|
||||
req.is('application/json');
|
||||
req.is('application/*');
|
||||
// => true
|
||||
|
||||
req.is('html');
|
||||
// => false
|
||||
~~~
|
||||
13
_includes/api/es/3x/req-originalUrl.md
Normal file
13
_includes/api/es/3x/req-originalUrl.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='req.originalUrl'>req.originalUrl</h3>
|
||||
|
||||
This property is much like `req.url`, however it retains
|
||||
the original request url, allowing you to rewrite `req.url`
|
||||
freely for internal routing purposes. For example the "mounting" feature
|
||||
of <a href="#app.use">app.use()</a> will rewrite `req.url` to
|
||||
strip the mount point.
|
||||
|
||||
~~~js
|
||||
// GET /search?q=something
|
||||
req.originalUrl
|
||||
// => "/search?q=something"
|
||||
~~~
|
||||
27
_includes/api/es/3x/req-param.md
Normal file
27
_includes/api/es/3x/req-param.md
Normal file
@@ -0,0 +1,27 @@
|
||||
<h3 id='req.param'>req.param(name)</h3>
|
||||
|
||||
Return the value of param `name` when present.
|
||||
|
||||
~~~js
|
||||
// ?name=tobi
|
||||
req.param('name')
|
||||
// => "tobi"
|
||||
|
||||
// POST name=tobi
|
||||
req.param('name')
|
||||
// => "tobi"
|
||||
|
||||
// /user/tobi for /user/:name
|
||||
req.param('name')
|
||||
// => "tobi"
|
||||
~~~
|
||||
|
||||
Lookup is performed in the following order:
|
||||
|
||||
* `req.params`
|
||||
* `req.body`
|
||||
* `req.query`
|
||||
|
||||
Direct access to `req.body`, `req.params`,
|
||||
and `req.query` should be favoured for clarity - unless
|
||||
you truly accept input from each object.
|
||||
22
_includes/api/es/3x/req-params.md
Normal file
22
_includes/api/es/3x/req-params.md
Normal file
@@ -0,0 +1,22 @@
|
||||
<h3 id='req.params'>req.params</h3>
|
||||
|
||||
This property is an array containing properties mapped to the named route "parameters".
|
||||
For example if you have the route `/user/:name`, then the "name" property
|
||||
is available to you as `req.params.name`. This object defaults to `{}`.
|
||||
|
||||
~~~js
|
||||
// GET /user/tj
|
||||
req.params.name
|
||||
// => "tj"
|
||||
~~~
|
||||
|
||||
When a regular expression is used for the route definition, capture groups
|
||||
are provided in the array using `req.params[N]`, where `N`
|
||||
is the nth capture group. This rule is applied to unnamed wild-card matches
|
||||
with string routes such as `/file/*`:
|
||||
|
||||
~~~js
|
||||
// GET /file/javascripts/jquery.js
|
||||
req.params[0]
|
||||
// => "javascripts/jquery.js"
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-path.md
Normal file
9
_includes/api/es/3x/req-path.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.path'>req.path</h3>
|
||||
|
||||
Returns the request URL pathname.
|
||||
|
||||
~~~js
|
||||
// example.com/users?sort=desc
|
||||
req.path
|
||||
// => "/users"
|
||||
~~~
|
||||
13
_includes/api/es/3x/req-protocol.md
Normal file
13
_includes/api/es/3x/req-protocol.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='req.protocol'>req.protocol</h3>
|
||||
|
||||
Return the protocol string "http" or "https"
|
||||
when requested with TLS. When the "trust proxy"
|
||||
setting is enabled the "X-Forwarded-Proto" header
|
||||
field will be trusted. If you're running behind
|
||||
a reverse proxy that supplies https for you this
|
||||
may be enabled.
|
||||
|
||||
js
|
||||
req.protocol
|
||||
// => "http"
|
||||
~~~
|
||||
20
_includes/api/es/3x/req-query.md
Normal file
20
_includes/api/es/3x/req-query.md
Normal file
@@ -0,0 +1,20 @@
|
||||
<h3 id='req.query'>req.query</h3>
|
||||
|
||||
This property is an object containing the parsed query-string,
|
||||
defaulting to `{}`.
|
||||
|
||||
~~~js
|
||||
// GET /search?q=tobi+ferret
|
||||
req.query.q
|
||||
// => "tobi ferret"
|
||||
|
||||
// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
|
||||
req.query.order
|
||||
// => "desc"
|
||||
|
||||
req.query.shoe.color
|
||||
// => "blue"
|
||||
|
||||
req.query.shoe.type
|
||||
// => "converse"
|
||||
~~~
|
||||
22
_includes/api/es/3x/req-route.md
Normal file
22
_includes/api/es/3x/req-route.md
Normal file
@@ -0,0 +1,22 @@
|
||||
<h3 id='req.route'>req.route</h3>
|
||||
|
||||
The currently matched `Route` containing
|
||||
several properties such as the route's original path
|
||||
string, the regexp generated, and so on.
|
||||
|
||||
~~~js
|
||||
app.get('/user/:id?', function(req, res){
|
||||
console.log(req.route);
|
||||
});
|
||||
~~~
|
||||
|
||||
Example output from the previous snippet:
|
||||
|
||||
~~~js
|
||||
{ path: '/user/:id?',
|
||||
method: 'get',
|
||||
callbacks: [ [Function] ],
|
||||
keys: [ { name: 'id', optional: true } ],
|
||||
regexp: /^\/user(?:\/([^\/]+?))?\/?$/i,
|
||||
params: [ id: '12' ] }
|
||||
~~~
|
||||
7
_includes/api/es/3x/req-secure.md
Normal file
7
_includes/api/es/3x/req-secure.md
Normal file
@@ -0,0 +1,7 @@
|
||||
<h3 id='req.secure'>req.secure</h3>
|
||||
|
||||
Check if a TLS connection is established. This is a short-hand for:
|
||||
|
||||
~~~js
|
||||
'https' == req.protocol;
|
||||
~~~
|
||||
15
_includes/api/es/3x/req-signedCookies.md
Normal file
15
_includes/api/es/3x/req-signedCookies.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='req.signedCookies'>req.signedCookies</h3>
|
||||
|
||||
This object requires the `cookieParser(secret)` middleware for use.
|
||||
It contains signed cookies sent by the user-agent, unsigned and ready for use.
|
||||
Signed cookies reside in a different object to show developer intent; otherwise,
|
||||
a malicious attack could be placed on `req.cookie` values (which are easy to spoof).
|
||||
Note that signing a cookie does not make it "hidden" or encrypted; this simply
|
||||
prevents tampering (because the secret used to sign is private). If no signed
|
||||
cookies are sent, it defaults to `{}`.
|
||||
|
||||
~~~js
|
||||
// Cookie: user=tobi.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3
|
||||
req.signedCookies.user
|
||||
// => "tobi"
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-stale.md
Normal file
9
_includes/api/es/3x/req-stale.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.stale'>req.stale</h3>
|
||||
|
||||
Check if the request is stale - aka Last-Modified and/or the ETag do not match,
|
||||
indicating that the resource is "stale".
|
||||
|
||||
~~~js
|
||||
req.stale
|
||||
// => true
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-subdomains.md
Normal file
9
_includes/api/es/3x/req-subdomains.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.subdomains'>req.subdomains</h3>
|
||||
|
||||
Return subdomains as an array.
|
||||
|
||||
~~~js
|
||||
// Host: "tobi.ferrets.example.com"
|
||||
req.subdomains
|
||||
// => ["ferrets", "tobi"]
|
||||
~~~
|
||||
9
_includes/api/es/3x/req-xhr.md
Normal file
9
_includes/api/es/3x/req-xhr.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='req.xhr'>req.xhr</h3>
|
||||
|
||||
Check if the request was issued with the "X-Requested-With"
|
||||
header field set to "XMLHttpRequest" (jQuery etc).
|
||||
|
||||
~~~js
|
||||
req.xhr
|
||||
// => true
|
||||
~~~
|
||||
109
_includes/api/es/3x/req.md
Normal file
109
_includes/api/es/3x/req.md
Normal file
@@ -0,0 +1,109 @@
|
||||
<h2>Request</h2>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-params.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-query.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-body.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-files.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-param.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-route.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-cookies.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-signedCookies.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-header.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-accepts.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-accepted.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-is.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-ip.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-ips.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-path.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-host.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-fresh.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-stale.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-xhr.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-protocol.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-secure.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-subdomains.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-originalUrl.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-acceptedLanguages.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-acceptedCharsets.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-acceptsCharset.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/req-acceptsLanguage.md %}
|
||||
</section>
|
||||
15
_includes/api/es/3x/res-attachment.md
Normal file
15
_includes/api/es/3x/res-attachment.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='res.attachment'>res.attachment([filename])</h3>
|
||||
|
||||
Sets the Content-Disposition header field to "attachment". If
|
||||
a `filename` is given then the Content-Type will be
|
||||
automatically set based on the extname via `res.type()`,
|
||||
and the Content-Disposition's "filename=" parameter will be set.
|
||||
|
||||
~~~js
|
||||
res.attachment();
|
||||
// Content-Disposition: attachment
|
||||
|
||||
res.attachment('path/to/logo.png');
|
||||
// Content-Disposition: attachment; filename="logo.png"
|
||||
// Content-Type: image/png
|
||||
~~~
|
||||
9
_includes/api/es/3x/res-charset.md
Normal file
9
_includes/api/es/3x/res-charset.md
Normal file
@@ -0,0 +1,9 @@
|
||||
<h3 id='res.charset'>res.charset</h3>
|
||||
|
||||
Assign the charset. Defaults to "utf-8".
|
||||
|
||||
~~~js
|
||||
res.charset = 'value';
|
||||
res.send('<p>some html</p>');
|
||||
// => Content-Type: text/html; charset=value
|
||||
~~~
|
||||
8
_includes/api/es/3x/res-clearCookie.md
Normal file
8
_includes/api/es/3x/res-clearCookie.md
Normal file
@@ -0,0 +1,8 @@
|
||||
<h3 id='res.clearCookie'>res.clearCookie(name, [options])</h3>
|
||||
|
||||
Clear cookie `name`. The `path` option defaults to "/".
|
||||
|
||||
~~~js
|
||||
res.cookie('name', 'tobi', { path: '/admin' });
|
||||
res.clearCookie('name', { path: '/admin' });
|
||||
~~~
|
||||
37
_includes/api/es/3x/res-cookie.md
Normal file
37
_includes/api/es/3x/res-cookie.md
Normal file
@@ -0,0 +1,37 @@
|
||||
<h3 id='res.cookie'>res.cookie(name, value, [options])</h3>
|
||||
|
||||
Set cookie `name` to `value`, which may be a string or object converted to JSON. The `path`
|
||||
option defaults to "/".
|
||||
|
||||
~~~js
|
||||
res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
|
||||
res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
|
||||
~~~
|
||||
|
||||
The `maxAge` option is a convenience option for setting "expires"
|
||||
relative to the current time in milliseconds. The following is equivalent to
|
||||
the previous example.
|
||||
|
||||
~~~js
|
||||
res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
|
||||
~~~
|
||||
|
||||
An object may be passed which is then serialized as JSON, which is
|
||||
automatically parsed by the `bodyParser()` middleware.
|
||||
|
||||
~~~js
|
||||
res.cookie('cart', { items: [1,2,3] });
|
||||
res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });
|
||||
~~~
|
||||
|
||||
Signed cookies are also supported through this method. Simply
|
||||
pass the `signed` option. When given `res.cookie()`
|
||||
will use the secret passed to `express.cookieParser(secret)`
|
||||
to sign the value.
|
||||
|
||||
~~~js
|
||||
res.cookie('name', 'tobi', { signed: true });
|
||||
~~~
|
||||
|
||||
Later you may access this value through the <a href="#req.signedCookies">req.signedCookie</a>
|
||||
object.
|
||||
26
_includes/api/es/3x/res-download.md
Normal file
26
_includes/api/es/3x/res-download.md
Normal file
@@ -0,0 +1,26 @@
|
||||
<h3 id='res.download'>res.download(path, [filename], [fn])</h3>
|
||||
|
||||
Transfer the file at `path` as an "attachment",
|
||||
typically browsers will prompt the user for download. The
|
||||
Content-Disposition "filename=" parameter, aka the one
|
||||
that will appear in the brower dialog is set to `path`
|
||||
by default, however you may provide an override `filename`.
|
||||
|
||||
When an error has ocurred or transfer is complete the optional
|
||||
callback `fn` is invoked. This method uses <a href="#res.sendfile">res.sendfile()</a>
|
||||
to transfer the file.
|
||||
|
||||
~~~js
|
||||
res.download('/report-12345.pdf');
|
||||
|
||||
res.download('/report-12345.pdf', 'report.pdf');
|
||||
|
||||
res.download('/report-12345.pdf', 'report.pdf', function(err){
|
||||
if (err) {
|
||||
// handle error, keep in mind the response may be partially-sent
|
||||
// so check res.headerSent
|
||||
} else {
|
||||
// decrement a download credit etc
|
||||
}
|
||||
});
|
||||
~~~
|
||||
52
_includes/api/es/3x/res-format.md
Normal file
52
_includes/api/es/3x/res-format.md
Normal file
@@ -0,0 +1,52 @@
|
||||
<h3 id='res.format'>res.format(object)</h3>
|
||||
|
||||
Performs content-negotiation on the request Accept header
|
||||
field when present. This method uses `req.accepted`, an array of
|
||||
acceptable types ordered by their quality values, otherwise the
|
||||
first callback is invoked. When no match is performed the server
|
||||
responds with 406 "Not Acceptable", or invokes the `default`
|
||||
callback.
|
||||
|
||||
The Content-Type is set for you when a callback is selected,
|
||||
however you may alter this within the callback using `res.set()`
|
||||
or `res.type()` etcetera.
|
||||
|
||||
The following example would respond with `{ "message": "hey" }`
|
||||
when the Accept header field is set to "application/json" or "*/json",
|
||||
however if "*/*" is given then "hey" will be the response.
|
||||
|
||||
~~~js
|
||||
res.format({
|
||||
'text/plain': function(){
|
||||
res.send('hey');
|
||||
},
|
||||
|
||||
'text/html': function(){
|
||||
res.send('<p>hey</p>');
|
||||
},
|
||||
|
||||
'application/json': function(){
|
||||
res.send({ message: 'hey' });
|
||||
}
|
||||
});
|
||||
~~~
|
||||
|
||||
In addition to canonicalized MIME types you may also
|
||||
use extnames mapped to these types, providing a slightly
|
||||
less verbose implementation:
|
||||
|
||||
~~~js
|
||||
res.format({
|
||||
text: function(){
|
||||
res.send('hey');
|
||||
},
|
||||
|
||||
html: function(){
|
||||
res.send('<p>hey</p>');
|
||||
},
|
||||
|
||||
json: function(){
|
||||
res.send({ message: 'hey' });
|
||||
}
|
||||
});
|
||||
~~~
|
||||
8
_includes/api/es/3x/res-get.md
Normal file
8
_includes/api/es/3x/res-get.md
Normal file
@@ -0,0 +1,8 @@
|
||||
<h3 id='res.get'>res.get(field)</h3>
|
||||
|
||||
Get the case-insensitive response header `field`.
|
||||
|
||||
~~~js
|
||||
res.get('Content-Type');
|
||||
// => "text/plain"
|
||||
~~~
|
||||
13
_includes/api/es/3x/res-json.md
Normal file
13
_includes/api/es/3x/res-json.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='res.json'>res.json([status|body], [body])</h3>
|
||||
|
||||
Send a JSON response. This method is identical
|
||||
to `res.send()` when an object or
|
||||
array is passed, however it may be used for
|
||||
explicit JSON conversion of non-objects (null, undefined, etc),
|
||||
though these are technically not valid JSON.
|
||||
|
||||
~~~js
|
||||
res.json(null)
|
||||
res.json({ user: 'tobi' })
|
||||
res.json(500, { error: 'message' })
|
||||
~~~
|
||||
33
_includes/api/es/3x/res-jsonp.md
Normal file
33
_includes/api/es/3x/res-jsonp.md
Normal file
@@ -0,0 +1,33 @@
|
||||
<h3 id='res.jsonp'>res.jsonp([status|body], [body])</h3>
|
||||
|
||||
Send a JSON response with JSONP support. This method is identical
|
||||
to `res.json()` however opts-in to JSONP callback
|
||||
support.
|
||||
|
||||
~~~js
|
||||
res.jsonp(null)
|
||||
// => null
|
||||
|
||||
res.jsonp({ user: 'tobi' })
|
||||
// => { "user": "tobi" }
|
||||
|
||||
res.jsonp(500, { error: 'message' })
|
||||
// => { "error": "message" }
|
||||
~~~
|
||||
|
||||
By default the JSONP callback name is simply `callback`,
|
||||
however you may alter this with the <a href="#app-settings">jsonp callback name</a>
|
||||
setting. The following are some examples of JSONP responses using the same
|
||||
code:
|
||||
|
||||
~~~js
|
||||
// ?callback=foo
|
||||
res.jsonp({ user: 'tobi' })
|
||||
// => foo({ "user": "tobi" })
|
||||
|
||||
app.set('jsonp callback name', 'cb');
|
||||
|
||||
// ?cb=foo
|
||||
res.jsonp(500, { error: 'message' })
|
||||
// => foo({ "error": "message" })
|
||||
~~~
|
||||
17
_includes/api/es/3x/res-links.md
Normal file
17
_includes/api/es/3x/res-links.md
Normal file
@@ -0,0 +1,17 @@
|
||||
<h3 id='res.links'>res.links(links)</h3>
|
||||
|
||||
Join the given `links` to populate the "Link" response header field.
|
||||
|
||||
~~~js
|
||||
res.links({
|
||||
next: 'http://api.example.com/users?page=2',
|
||||
last: 'http://api.example.com/users?page=5'
|
||||
});
|
||||
~~~
|
||||
|
||||
p yields:
|
||||
|
||||
~~~js
|
||||
Link: <http://api.example.com/users?page=2>; rel="next",
|
||||
<http://api.example.com/users?page=5>; rel="last"
|
||||
~~~
|
||||
16
_includes/api/es/3x/res-locals.md
Normal file
16
_includes/api/es/3x/res-locals.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='res.locals'>res.locals</h3>
|
||||
|
||||
Response local variables are scoped to the request, thus only
|
||||
available to the view(s) rendered during that request / response
|
||||
cycle, if any. Otherwise this API is identical to <a href="#app.locals">app.locals</a>.
|
||||
|
||||
This object is useful for exposing request-level information such as the
|
||||
request pathname, authenticated user, user settings etcetera.
|
||||
|
||||
~~~js
|
||||
app.use(function(req, res, next){
|
||||
res.locals.user = req.user;
|
||||
res.locals.authenticated = ! req.user.anonymous;
|
||||
next();
|
||||
});
|
||||
~~~
|
||||
21
_includes/api/es/3x/res-location.md
Normal file
21
_includes/api/es/3x/res-location.md
Normal file
@@ -0,0 +1,21 @@
|
||||
<h3 id='res.location'>res.location</h3>
|
||||
|
||||
Set the location header.
|
||||
|
||||
~~~js
|
||||
res.location('/foo/bar');
|
||||
res.location('foo/bar');
|
||||
res.location('http://example.com');
|
||||
res.location('../login');
|
||||
res.location('back');
|
||||
~~~
|
||||
|
||||
You can use the same kind of `urls` as in `res.redirect()`.
|
||||
|
||||
For example, if your application is mounted at `/blog`,
|
||||
the following would set the `location` header to
|
||||
`/blog/admin`:
|
||||
|
||||
~~~js
|
||||
res.location('admin')
|
||||
~~~
|
||||
51
_includes/api/es/3x/res-redirect.md
Normal file
51
_includes/api/es/3x/res-redirect.md
Normal file
@@ -0,0 +1,51 @@
|
||||
<h3 id='res.redirect'>res.redirect([status], url)</h3>
|
||||
|
||||
Redirect to the given `url` with optional `status` code
|
||||
defaulting to 302 "Found".
|
||||
|
||||
~~~js
|
||||
res.redirect('/foo/bar');
|
||||
res.redirect('http://example.com');
|
||||
res.redirect(301, 'http://example.com');
|
||||
res.redirect('../login');
|
||||
~~~
|
||||
|
||||
Express supports a few forms of redirection, first being
|
||||
a fully qualified URI for redirecting to a different site:
|
||||
|
||||
~~~js
|
||||
res.redirect('http://google.com');
|
||||
~~~
|
||||
|
||||
The second form is the pathname-relative redirect, for example
|
||||
if you were on `http://example.com/admin/post/new`, the
|
||||
following redirect to `/admin` would land you at `http://example.com/admin`:
|
||||
|
||||
~~~js
|
||||
res.redirect('/admin');
|
||||
~~~
|
||||
|
||||
This next redirect is relative to the `mount` point of the application. For example
|
||||
if you have a blog application mounted at `/blog`, ideally it has no knowledge of
|
||||
where it was mounted, so where a redirect of `/admin/post/new` would simply give you
|
||||
`http://example.com/admin/post/new`, the following mount-relative redirect would give
|
||||
you `http://example.com/blog/admin/post/new`:
|
||||
|
||||
~~~js
|
||||
res.redirect('admin/post/new');
|
||||
~~~
|
||||
|
||||
Pathname relative redirects are also possible. If you were
|
||||
on `http://example.com/admin/post/new`, the following redirect
|
||||
would land you at `http//example.com/admin/post`:
|
||||
|
||||
~~~js
|
||||
res.redirect('..');
|
||||
~~~
|
||||
|
||||
The final special-case is a `back` redirect, redirecting back to
|
||||
the Referer (or Referrer), defaulting to `/` when missing.
|
||||
|
||||
~~~js
|
||||
res.redirect('back');
|
||||
~~~
|
||||
16
_includes/api/es/3x/res-render.md
Normal file
16
_includes/api/es/3x/res-render.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='res.render'>res.render(view, [locals], callback)</h3>
|
||||
|
||||
Render a `view` with a callback responding with
|
||||
the rendered string. When an error occurs `next(err)`
|
||||
is invoked internally. When a callback is provided both the possible error
|
||||
and rendered string are passed, and no automated response is performed.
|
||||
|
||||
~~~js
|
||||
res.render('index', function(err, html){
|
||||
// ...
|
||||
});
|
||||
|
||||
res.render('user', { name: 'Tobi' }, function(err, html){
|
||||
// ...
|
||||
});
|
||||
~~~
|
||||
53
_includes/api/es/3x/res-send.md
Normal file
53
_includes/api/es/3x/res-send.md
Normal file
@@ -0,0 +1,53 @@
|
||||
<h3 id='res.send'>res.send([body|status], [body])</h3>
|
||||
|
||||
Send a response.
|
||||
|
||||
~~~js
|
||||
res.send(new Buffer('whoop'));
|
||||
res.send({ some: 'json' });
|
||||
res.send('<p>some html</p>');
|
||||
res.send(404, 'Sorry, we cannot find that!');
|
||||
res.send(500, { error: 'something blew up' });
|
||||
res.send(200);
|
||||
~~~
|
||||
|
||||
This method performs a myriad of
|
||||
useful tasks for simple non-streaming responses such
|
||||
as automatically assigning the Content-Length unless
|
||||
previously defined and providing automatic <em>HEAD</em> and
|
||||
HTTP cache freshness support.
|
||||
|
||||
When a `Buffer` is given
|
||||
the Content-Type is set to "application/octet-stream"
|
||||
unless previously defined as shown below:
|
||||
|
||||
~~~js
|
||||
res.set('Content-Type', 'text/html');
|
||||
res.send(new Buffer('<p>some html</p>'));
|
||||
~~~
|
||||
|
||||
When a `String` is given the
|
||||
Content-Type is set defaulted to "text/html":
|
||||
|
||||
~~~js
|
||||
res.send('<p>some html</p>');
|
||||
~~~
|
||||
|
||||
When an `Array` or `Object` is
|
||||
given Express will respond with the JSON representation:
|
||||
|
||||
~~~js
|
||||
res.send({ user: 'tobi' })
|
||||
res.send([1,2,3])
|
||||
~~~
|
||||
|
||||
Finally when a `Number` is given without
|
||||
any of the previously mentioned bodies, then a response
|
||||
body string is assigned for you. For example 200 will
|
||||
respond will the text "OK", and 404 "Not Found" and so on.
|
||||
|
||||
~~~js
|
||||
res.send(200)
|
||||
res.send(404)
|
||||
res.send(500)
|
||||
~~~
|
||||
30
_includes/api/es/3x/res-sendfile.md
Normal file
30
_includes/api/es/3x/res-sendfile.md
Normal file
@@ -0,0 +1,30 @@
|
||||
<h3 id='res.sendfile'>res.sendfile(path, [options], [fn]])</h3>
|
||||
|
||||
Transfer the file at the given `path`.
|
||||
|
||||
Automatically defaults the Content-Type response header field based
|
||||
on the filename's extension. The callback `fn(err)` is
|
||||
invoked when the transfer is complete or when an error occurs.
|
||||
|
||||
Options:
|
||||
|
||||
* `maxAge` in milliseconds defaulting to 0
|
||||
* `root` root directory for relative filenames
|
||||
|
||||
This method provides fine-grained support for file serving
|
||||
as illustrated in the following example:
|
||||
|
||||
~~~js
|
||||
app.get('/user/:uid/photos/:file', function(req, res){
|
||||
var uid = req.params.uid
|
||||
, file = req.params.file;
|
||||
|
||||
req.user.mayViewFilesFrom(uid, function(yes){
|
||||
if (yes) {
|
||||
res.sendfile('/uploads/' + uid + '/' + file);
|
||||
} else {
|
||||
res.send(403, 'Sorry! you cant see that.');
|
||||
}
|
||||
});
|
||||
});
|
||||
~~~
|
||||
15
_includes/api/es/3x/res-set.md
Normal file
15
_includes/api/es/3x/res-set.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='res.set'>res.set(field, [value])</h3>
|
||||
|
||||
Set header `field` to `value`, or pass an object to set multiple fields at once.
|
||||
|
||||
~~~js
|
||||
res.set('Content-Type', 'text/plain');
|
||||
|
||||
res.set({
|
||||
'Content-Type': 'text/plain',
|
||||
'Content-Length': '123',
|
||||
'ETag': '12345'
|
||||
})
|
||||
~~~
|
||||
|
||||
Aliased as `res.header(field, [value])`.
|
||||
7
_includes/api/es/3x/res-status.md
Normal file
7
_includes/api/es/3x/res-status.md
Normal file
@@ -0,0 +1,7 @@
|
||||
<h3 id='res.status'>res.status(code)</h3>
|
||||
|
||||
Chainable alias of node's `res.statusCode=`.
|
||||
|
||||
~~~js
|
||||
res.status(404).sendfile('path/to/404.png');
|
||||
~~~
|
||||
15
_includes/api/es/3x/res-type.md
Normal file
15
_includes/api/es/3x/res-type.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='res.type'>res.type(type)</h3>
|
||||
|
||||
Sets the Content-Type to the mime lookup of `type`,
|
||||
or when "/" is present the Content-Type is simply set to this
|
||||
literal value.
|
||||
|
||||
~~~js
|
||||
res.type('.html');
|
||||
res.type('html');
|
||||
res.type('json');
|
||||
res.type('application/json');
|
||||
res.type('png');
|
||||
~~~
|
||||
|
||||
p Aliased as `res.contentType(type)`.
|
||||
77
_includes/api/es/3x/res.md
Normal file
77
_includes/api/es/3x/res.md
Normal file
@@ -0,0 +1,77 @@
|
||||
<h2>Response</h2>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-status.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-set.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-get.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-cookie.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-clearCookie.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-redirect.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-location.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-charset.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-send.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-json.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-jsonp.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-type.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-format.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-attachment.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-sendfile.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-download.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-links.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-locals.md %}
|
||||
</section>
|
||||
|
||||
<section markdown="1">
|
||||
{% include api/{{ page.lang }}/3x/res-render.md %}
|
||||
</section>
|
||||
75
_includes/api/es/4x/app-METHOD.md
Normal file
75
_includes/api/es/4x/app-METHOD.md
Normal file
@@ -0,0 +1,75 @@
|
||||
<h3 id='app.METHOD'>app.METHOD(path, callback [, callback ...])</h3>
|
||||
|
||||
Routes an HTTP request, where METHOD is the HTTP method of the request, such as GET,
|
||||
PUT, POST, and so on, in lowercase. Thus, the actual methods are `app.get()`,
|
||||
`app.post()`, `app.put()`, and so on. See below for the complete list.
|
||||
|
||||
For more information, see the [routing guide](/guide/routing.html).
|
||||
|
||||
Express supports the following routing methods corresponding to the HTTP methods of the same names:
|
||||
|
||||
<table style="border: 0px; background: none">
|
||||
<tr>
|
||||
<td style="background: none; border: 0px;" markdown="1">
|
||||
* `checkout`
|
||||
* `connect`
|
||||
* `copy`
|
||||
* `delete`
|
||||
* `get`
|
||||
* `head`
|
||||
* `lock`
|
||||
* `merge`
|
||||
* `mkactivity`
|
||||
</td>
|
||||
<td style="background: none; border: 0px;" markdown="1">
|
||||
* `mkcol`
|
||||
* `move`
|
||||
* `m-search`
|
||||
* `notify`
|
||||
* `options`
|
||||
* `patch`
|
||||
* `post`
|
||||
* `propfind`
|
||||
* `proppatch`
|
||||
</td>
|
||||
<td style="background: none; border: 0px;" markdown="1">
|
||||
* `purge`
|
||||
* `put`
|
||||
* `report`
|
||||
* `search`
|
||||
* `subscribe`
|
||||
* `trace`
|
||||
* `unlock`
|
||||
* `unsubscribe`
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<div class="doc-box doc-info" markdown="1">
|
||||
To route methods which translate to invalid JavaScript variable names, use the bracket notation. For example,
|
||||
`app['m-search']('/', function ...`.
|
||||
</div>
|
||||
|
||||
You can provide multiple callback functions that behave just like middleware, except
|
||||
that these callbacks can invoke `next('route')` to bypass
|
||||
the remaining route callback(s). You can use this mechanism to impose pre-conditions
|
||||
on a route, then pass control to subsequent routes if there is no reason to proceed with the current route.
|
||||
|
||||
<div class="doc-box doc-info" markdown="1">
|
||||
The API documentation has explicit entries only for the most popular HTTP methods `app.get()`,
|
||||
`app.post()`, `app.put()`, and `app.delete()`.
|
||||
However, the other methods listed above work in exactly the same way.
|
||||
</div>
|
||||
|
||||
There is a special routing method, `app.all()`, that is not derived from any HTTP method.
|
||||
It loads middleware at a path for all request methods.
|
||||
|
||||
In the following example, the handler is executed for requests to "/secret" whether using
|
||||
GET, POST, PUT, DELETE, or any other HTTP request method.
|
||||
|
||||
~~~js
|
||||
app.all('/secret', function (req, res, next) {
|
||||
console.log('Accessing the secret section ...')
|
||||
next() // pass control to the next handler
|
||||
})
|
||||
~~~
|
||||
31
_includes/api/es/4x/app-all.md
Normal file
31
_includes/api/es/4x/app-all.md
Normal file
@@ -0,0 +1,31 @@
|
||||
<h3 id='app.all'>app.all(path, callback [, callback ...])</h3>
|
||||
|
||||
This method is like the standard [app.METHOD()](#app.METHOD) methods,
|
||||
except it matches all HTTP verbs.
|
||||
|
||||
It's useful for mapping "global" logic for specific path prefixes or arbitrary matches.
|
||||
For example, if you put the following at the top of all other
|
||||
route definitions, it requires that all routes from that point on
|
||||
require authentication, and automatically load a user. Keep in mind
|
||||
that these callbacks do not have to act as end-points: `loadUser`
|
||||
can perform a task, then call `next()` to continue matching subsequent
|
||||
routes.
|
||||
|
||||
~~~js
|
||||
app.all('*', requireAuthentication, loadUser);
|
||||
~~~
|
||||
|
||||
Or the equivalent:
|
||||
|
||||
~~~js
|
||||
app.all('*', requireAuthentication)
|
||||
app.all('*', loadUser);
|
||||
~~~
|
||||
|
||||
Another example is white-listed "global" functionality.
|
||||
The example is much like before, however it only restricts paths that start with
|
||||
"/api":
|
||||
|
||||
~~~js
|
||||
app.all('/api/*', requireAuthentication);
|
||||
~~~
|
||||
15
_includes/api/es/4x/app-delete-method.md
Normal file
15
_includes/api/es/4x/app-delete-method.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='app.delete.method'>app.delete(path, callback [, callback ...])</h3>
|
||||
|
||||
Routes HTTP DELETE requests to the specified path with the specified callback functions.
|
||||
For more information, see the [routing guide](/guide/routing.html).
|
||||
|
||||
You can provide multiple callback functions that behave just like middleware, except
|
||||
these callbacks can invoke `next('route')` to bypass the remaining route
|
||||
callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control
|
||||
to subsequent routes if there's no reason to proceed with the current route.
|
||||
|
||||
~~~js
|
||||
app.delete('/', function (req, res) {
|
||||
res.send('DELETE request to homepage');
|
||||
});
|
||||
~~~
|
||||
12
_includes/api/es/4x/app-disable.md
Normal file
12
_includes/api/es/4x/app-disable.md
Normal file
@@ -0,0 +1,12 @@
|
||||
<h3 id='app.disable'>app.disable(name)</h3>
|
||||
|
||||
Sets the Boolean setting `name` to `false`, where `name` is one of the properties from the [app settings table](#app.settings.table).
|
||||
Calling `app.set('foo', false)` for a Boolean property is the same as calling `app.disable('foo')`.
|
||||
|
||||
For example:
|
||||
|
||||
~~~js
|
||||
app.disable('trust proxy');
|
||||
app.get('trust proxy');
|
||||
// => false
|
||||
~~~
|
||||
13
_includes/api/es/4x/app-disabled.md
Normal file
13
_includes/api/es/4x/app-disabled.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='app.disabled'>app.disabled(name)</h3>
|
||||
|
||||
Returns `true` if the Boolean setting `name` is disabled (`false`), where `name` is one of the properties from
|
||||
the [app settings table](#app.settings.table).
|
||||
|
||||
~~~js
|
||||
app.disabled('trust proxy');
|
||||
// => true
|
||||
|
||||
app.enable('trust proxy');
|
||||
app.disabled('trust proxy');
|
||||
// => false
|
||||
~~~
|
||||
10
_includes/api/es/4x/app-enable.md
Normal file
10
_includes/api/es/4x/app-enable.md
Normal file
@@ -0,0 +1,10 @@
|
||||
<h3 id='app.enable'>app.enable(name)</h3>
|
||||
|
||||
Sets the Boolean setting `name` to `true`, where `name` is one of the properties from the [app settings table](#app.settings.table).
|
||||
Calling `app.set('foo', true)` for a Boolean property is the same as calling `app.enable('foo')`.
|
||||
|
||||
~~~js
|
||||
app.enable('trust proxy');
|
||||
app.get('trust proxy');
|
||||
// => true
|
||||
~~~
|
||||
13
_includes/api/es/4x/app-enabled.md
Normal file
13
_includes/api/es/4x/app-enabled.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='app.enabled'>app.enabled(name)</h3>
|
||||
|
||||
Returns `true` if the setting `name` is enabled (`true`), where `name` is one of the
|
||||
properties from the [app settings table](#app.settings.table).
|
||||
|
||||
~~~js
|
||||
app.enabled('trust proxy');
|
||||
// => false
|
||||
|
||||
app.enable('trust proxy');
|
||||
app.enabled('trust proxy');
|
||||
// => true
|
||||
~~~
|
||||
36
_includes/api/es/4x/app-engine.md
Normal file
36
_includes/api/es/4x/app-engine.md
Normal file
@@ -0,0 +1,36 @@
|
||||
<h3 id='app.engine'>app.engine(ext, callback)</h3>
|
||||
|
||||
Registers the given template engine `callback` as `ext`.
|
||||
|
||||
By default, Express will `require()` the engine based on the file extension.
|
||||
For example, if you try to render a "foo.jade" file, Express invokes the
|
||||
following internally, and caches the `require()` on subsequent calls to increase
|
||||
performance.
|
||||
|
||||
~~~js
|
||||
app.engine('jade', require('jade').__express);
|
||||
~~~
|
||||
|
||||
Use this method for engines that do not provide `.__express` out of the box,
|
||||
or if you wish to "map" a different extension to the template engine.
|
||||
|
||||
For example, to map the EJS template engine to ".html" files:
|
||||
|
||||
~~~js
|
||||
app.engine('html', require('ejs').renderFile);
|
||||
~~~
|
||||
|
||||
In this case, EJS provides a `.renderFile()` method with
|
||||
the same signature that Express expects: `(path, options, callback)`,
|
||||
though note that it aliases this method as `ejs.__express` internally
|
||||
so if you're using ".ejs" extensions you don't need to do anything.
|
||||
|
||||
Some template engines do not follow this convention. The
|
||||
[consolidate.js](https://github.com/tj/consolidate.js) library maps Node template engines to follow this convention,
|
||||
so they work seemlessly with Express.
|
||||
|
||||
~~~js
|
||||
var engines = require('consolidate');
|
||||
app.engine('haml', engines.haml);
|
||||
app.engine('html', engines.hogan);
|
||||
~~~
|
||||
15
_includes/api/es/4x/app-get-method.md
Normal file
15
_includes/api/es/4x/app-get-method.md
Normal file
@@ -0,0 +1,15 @@
|
||||
<h3 id='app.get.method'>app.get(path, callback [, callback ...])</h3>
|
||||
|
||||
Routes HTTP GET requests to the specified path with the specified callback functions.
|
||||
For more information, see the [routing guide](/guide/routing.html).
|
||||
|
||||
You can provide multiple callback functions that behave just like middleware, except
|
||||
these callbacks can invoke `next('route')` to bypass the remaining route callback(s).
|
||||
You can use this mechanism to impose pre-conditions on a route, then pass control to
|
||||
subsequent routes if there's no reason to proceed with the current route.
|
||||
|
||||
~~~js
|
||||
app.get('/', function (req, res) {
|
||||
res.send('GET request to homepage');
|
||||
});
|
||||
~~~
|
||||
13
_includes/api/es/4x/app-get.md
Normal file
13
_includes/api/es/4x/app-get.md
Normal file
@@ -0,0 +1,13 @@
|
||||
<h3 id='app.get'>app.get(name)</h3>
|
||||
|
||||
Returns the value of `name` app setting, where `name` is one of strings in the
|
||||
[app settings table](#app.settings.table). For example:
|
||||
|
||||
~~~js
|
||||
app.get('title');
|
||||
// => undefined
|
||||
|
||||
app.set('title', 'My Site');
|
||||
app.get('title');
|
||||
// => "My Site"
|
||||
~~~
|
||||
35
_includes/api/es/4x/app-listen.md
Normal file
35
_includes/api/es/4x/app-listen.md
Normal file
@@ -0,0 +1,35 @@
|
||||
<h3 id='app.listen'>app.listen(port, [hostname], [backlog], [callback])</h3>
|
||||
|
||||
Binds and listens for connections on the specified host and port.
|
||||
This method is identical to Node's [http.Server.listen()](http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback).
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
app.listen(3000);
|
||||
~~~
|
||||
|
||||
The `app` returned by `express()` is in fact a JavaScript
|
||||
`Function`, designed to be passed to Node's HTTP servers as a callback
|
||||
to handle requests. This makes it easy to provide both HTTP and HTTPS versions of
|
||||
your app with the same code base, as the app does not inherit from these
|
||||
(it is simply a callback):
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
var https = require('https');
|
||||
var http = require('http');
|
||||
var app = express();
|
||||
|
||||
http.createServer(app).listen(80);
|
||||
https.createServer(options, app).listen(443);
|
||||
~~~
|
||||
|
||||
The `app.listen()` method is a convenience method for the following (for HTTP only):
|
||||
|
||||
~~~js
|
||||
app.listen = function() {
|
||||
var server = http.createServer(this);
|
||||
return server.listen.apply(server, arguments);
|
||||
};
|
||||
~~~
|
||||
26
_includes/api/es/4x/app-locals.md
Normal file
26
_includes/api/es/4x/app-locals.md
Normal file
@@ -0,0 +1,26 @@
|
||||
<h3 id='app.locals'>app.locals</h3>
|
||||
|
||||
The `app.locals` object is a JavaScript object, and its
|
||||
properties are local variables within the application.
|
||||
|
||||
~~~js
|
||||
app.locals.title
|
||||
// => 'My App'
|
||||
|
||||
app.locals.email
|
||||
// => 'me@myapp.com'
|
||||
~~~
|
||||
|
||||
Once set, the value of `app.locals` properties persist throughout the life of the application,
|
||||
in contrast with [res.locals](#res.locals) properties that
|
||||
are valid only for the lifetime of the request.
|
||||
|
||||
You can accesss local variables in templates rendered within the application.
|
||||
This is useful for providing helper functions to templates, as well as app-level data.
|
||||
Note, however, that you cannot access local variables in middleware.
|
||||
|
||||
~~~js
|
||||
app.locals.title = 'My App';
|
||||
app.locals.strftime = require('strftime');
|
||||
app.locals.email = 'me@myapp.com';
|
||||
~~~
|
||||
43
_includes/api/es/4x/app-mountpath.md
Normal file
43
_includes/api/es/4x/app-mountpath.md
Normal file
@@ -0,0 +1,43 @@
|
||||
<h3 id='app.mountpath'>app.mountpath</h3>
|
||||
|
||||
The `app.mountpath` property is the path pattern(s) on which a sub app was mounted.
|
||||
|
||||
<div class="doc-box doc-info" markdown="1">
|
||||
A sub app is an instance of `express` which may be used for handling the request to a route.
|
||||
</div>
|
||||
|
||||
~~~js
|
||||
var express = require('express');
|
||||
|
||||
var app = express(); // the main app
|
||||
var admin = express(); // the sub app
|
||||
|
||||
admin.get('/', function (req, res) {
|
||||
console.log(admin.mountpath); // /admin
|
||||
res.send('Admin Homepage');
|
||||
})
|
||||
|
||||
app.use('/admin', admin); // mount the sub app
|
||||
~~~
|
||||
|
||||
It is similar to the [baseUrl](#req.baseUrl) property of the `req` object, except `req.baseUrl` returns the matched URL path, instead of the matched pattern(s).
|
||||
|
||||
If a sub-app is mounted on multiple path patterns, `app.mountpath` returns the list of patterns it is mounted on, as shown in the following example.
|
||||
|
||||
~~~js
|
||||
var admin = express();
|
||||
|
||||
admin.get('/', function (req, res) {
|
||||
console.log(admin.mountpath); // [ '/adm*n', '/manager' ]
|
||||
res.send('Admin Homepage');
|
||||
})
|
||||
|
||||
var secret = express();
|
||||
secret.get('/', function (req, res) {
|
||||
console.log(secret.mountpath); // /secr*t
|
||||
res.send('Admin Secret');
|
||||
});
|
||||
|
||||
admin.use('/secr*t', secret); // load the 'secret' router on '/secr*t', on the 'admin' sub app
|
||||
app.use(['/adm*n', '/manager'], admin); // load the 'admin' router on '/adm*n' and '/manager', on the parent app
|
||||
~~~
|
||||
18
_includes/api/es/4x/app-onmount.md
Normal file
18
_includes/api/es/4x/app-onmount.md
Normal file
@@ -0,0 +1,18 @@
|
||||
<h3 id='app.onmount'>app.on('mount', callback(parent))</h3>
|
||||
|
||||
The `mount` event is fired on a sub-app, when it is mounted on a parent app. The parent app is passed to the callback function.
|
||||
|
||||
~~~js
|
||||
var admin = express();
|
||||
|
||||
admin.on('mount', function (parent) {
|
||||
console.log('Admin Mounted');
|
||||
console.log(parent); // refers to the parent app
|
||||
});
|
||||
|
||||
admin.get('/', function (req, res) {
|
||||
res.send('Admin Homepage');
|
||||
});
|
||||
|
||||
app.use('/admin', admin);
|
||||
~~~
|
||||
100
_includes/api/es/4x/app-param.md
Normal file
100
_includes/api/es/4x/app-param.md
Normal file
@@ -0,0 +1,100 @@
|
||||
<h3 id='app.param'>app.param([name], callback)</h3>
|
||||
|
||||
Add callback triggers to route parameters, where `name` is the name of the parameter or an array of them, and `function` is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, and the value of the parameter, in that order.
|
||||
|
||||
For example, when `:user` is present in a route path, you may map user loading logic to automatically provide `req.user` to the route, or perform validations on the parameter input.
|
||||
|
||||
~~~js
|
||||
app.param('user', function(req, res, next, id) {
|
||||
|
||||
// try to get the user details from the User model and attach it to the request object
|
||||
User.find(id, function(err, user) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else if (user) {
|
||||
req.user = user;
|
||||
next();
|
||||
} else {
|
||||
next(new Error('failed to load user'));
|
||||
}
|
||||
});
|
||||
});
|
||||
~~~
|
||||
|
||||
Param callback functions are local to the router on which they are defined. They are not inherited by mounted apps or routers. Hence, param callbacks defined on `app` will be triggered only by route parameters defined on `app` routes.
|
||||
|
||||
A param callback will be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following example.
|
||||
|
||||
~~~js
|
||||
app.param('id', function (req, res, next, id) {
|
||||
console.log('CALLED ONLY ONCE');
|
||||
next();
|
||||
})
|
||||
|
||||
app.get('/user/:id', function (req, res, next) {
|
||||
console.log('although this matches');
|
||||
next();
|
||||
});
|
||||
|
||||
app.get('/user/:id', function (req, res) {
|
||||
console.log('and this matches too');
|
||||
res.end();
|
||||
});
|
||||
~~~
|
||||
|
||||
<div class="doc-box doc-warn" markdown="1">
|
||||
`app.param(callback)` is deprecated as of v4.11.0.
|
||||
</div>
|
||||
|
||||
By passing only a callback function, you can alter the `app.param()` API. For example the [express-params](http://github.com/expressjs/express-params) defines the following callback which allows you to restrict parameters to a given regular expression.
|
||||
|
||||
<div class="doc-box doc-info" markdown="1">
|
||||
The code in the next section can be migrated using the following, without the use of `app.param(callback)`:
|
||||
|
||||
<pre>
|
||||
router.get('/user/:id([0-9]+)', function(req, res){
|
||||
res.send('user ' + req.params.id);
|
||||
});
|
||||
|
||||
router.get('/range/:range(\\w+\.\.\\w+)', function(req, res){
|
||||
var range = req.params.range.split('..');
|
||||
res.send('from ' + range[0] + ' to ' + range[1]);
|
||||
});
|
||||
</pre>
|
||||
|
||||
</div>
|
||||
|
||||
~~~js
|
||||
app.param(function(name, fn) {
|
||||
if (fn instanceof RegExp) {
|
||||
return function(req, res, next, val) {
|
||||
var captures;
|
||||
if (captures = fn.exec(String(val))) {
|
||||
req.params[name] = captures;
|
||||
next();
|
||||
} else {
|
||||
next('route');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
~~~
|
||||
|
||||
The method could now be used to effectively validate parameters (and optionally parse them to provide capture groups):
|
||||
|
||||
~~~js
|
||||
// validation rule for id: should be one or more digits
|
||||
app.param('id', /^\d+$/);
|
||||
|
||||
app.get('/user/:id', function(req, res) {
|
||||
res.send('user ' + req.params.id);
|
||||
});
|
||||
|
||||
// validation rule for range: should start with one more alphanumeric characters, followed by two dots, and end with one more alphanumeric characters
|
||||
app.param('range', /^(\w+)\.\.(\w+)?$/);
|
||||
|
||||
app.get('/range/:range', function(req, res) {
|
||||
var range = req.params.range;
|
||||
res.send('from ' + range[1] + ' to ' + range[2]);
|
||||
});
|
||||
~~~
|
||||
19
_includes/api/es/4x/app-path.md
Normal file
19
_includes/api/es/4x/app-path.md
Normal file
@@ -0,0 +1,19 @@
|
||||
<h3 id='app.path'>app.path()</h3>
|
||||
|
||||
Returns the canonical path of the app, a string.
|
||||
|
||||
~~~js
|
||||
var app = express()
|
||||
, blog = express()
|
||||
, blogAdmin = express();
|
||||
|
||||
app.use('/blog', blog);
|
||||
blog.use('/admin', blogAdmin);
|
||||
|
||||
console.log(app.path()); // ''
|
||||
console.log(blog.path()); // '/blog'
|
||||
console.log(blogAdmin.path()); // '/blog/admin'
|
||||
~~~
|
||||
|
||||
The behavior of this method can become very complicated in complex cases of mounted apps:
|
||||
it is usually better to use [req.baseUrl](#req.baseUrl) to get the canonical path of the app.
|
||||
16
_includes/api/es/4x/app-post-method.md
Normal file
16
_includes/api/es/4x/app-post-method.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='app.post.method'>app.post(path, callback [, callback ...])</h3>
|
||||
|
||||
Routes HTTP POST requests to the specified path with the specified callback functions.
|
||||
For more information, see the [routing guide](/guide/routing.html).
|
||||
|
||||
You can provide multiple callback functions that behave just like middleware,
|
||||
except that these callbacks can invoke `next('route')` to bypass the
|
||||
remaining route callback(s). You can use this mechanism to impose pre-conditions on
|
||||
a route, then pass control to subsequent routes if there's no reason to proceed with
|
||||
the current route.
|
||||
|
||||
~~~js
|
||||
app.post('/', function (req, res) {
|
||||
res.send('POST request to homepage');
|
||||
});
|
||||
~~~
|
||||
16
_includes/api/es/4x/app-put-method.md
Normal file
16
_includes/api/es/4x/app-put-method.md
Normal file
@@ -0,0 +1,16 @@
|
||||
<h3 id='app.put.method'>app.put(path, callback [, callback ...])</h3>
|
||||
|
||||
Routes HTTP PUT requests to the specified path with the specified callback functions.
|
||||
For more information, see the [routing guide](/guide/routing.html).
|
||||
|
||||
You can provide multiple callback functions that behave just like middleware,
|
||||
except that these callbacks can invoke `next('route')` to bypass the
|
||||
remaining route callback(s). You can use this mechanism to impose pre-conditions on
|
||||
a route, then pass control to subsequent routes if there's no reason to proceed with
|
||||
the current route.
|
||||
|
||||
~~~js
|
||||
app.put('/', function (req, res) {
|
||||
res.send('PUT request to homepage');
|
||||
});
|
||||
~~~
|
||||
25
_includes/api/es/4x/app-render.md
Normal file
25
_includes/api/es/4x/app-render.md
Normal file
@@ -0,0 +1,25 @@
|
||||
<h3 id='app.render'>app.render(view, [locals], callback)</h3>
|
||||
|
||||
Returns the rendered HTML of a view via the `callback` function. It accepts an optional parameter
|
||||
that is an object containing local variables for the view. It is like [res.render()](#res.render),
|
||||
except it cannot send the rendered view to the client on its own.
|
||||
|
||||
<div class="doc-box doc-info" markdown="1">
|
||||
Think of `app.render()` as a utility function for generating rendered view strings.
|
||||
Internally `res.render()` uses `app.render()` to render views.
|
||||
</div>
|
||||
|
||||
<div class="doc-box doc-notice" markdown="1">
|
||||
The local variable `cache` is reserved for enabling view cache. Set it to `true`, if you want to
|
||||
cache view during development; view caching is enabled in production by default.
|
||||
</div>
|
||||
|
||||
~~~js
|
||||
app.render('email', function(err, html){
|
||||
// ...
|
||||
});
|
||||
|
||||
app.render('email', { name: 'Tobi' }, function(err, html){
|
||||
// ...
|
||||
});
|
||||
~~~
|
||||
20
_includes/api/es/4x/app-route.md
Normal file
20
_includes/api/es/4x/app-route.md
Normal file
@@ -0,0 +1,20 @@
|
||||
<h3 id='app.route'>app.route(path)</h3>
|
||||
|
||||
Returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware.
|
||||
Use `app.route()` to avoid duplicate route names (and thus typo errors).
|
||||
|
||||
~~~js
|
||||
var app = express();
|
||||
|
||||
app.route('/events')
|
||||
.all(function(req, res, next) {
|
||||
// runs for all HTTP verbs first
|
||||
// think of it as route specific middleware!
|
||||
})
|
||||
.get(function(req, res, next) {
|
||||
res.json(...);
|
||||
})
|
||||
.post(function(req, res, next) {
|
||||
// maybe add a new event...
|
||||
})
|
||||
~~~
|
||||
19
_includes/api/es/4x/app-set.md
Normal file
19
_includes/api/es/4x/app-set.md
Normal file
@@ -0,0 +1,19 @@
|
||||
<h3 id='app.set'>app.set(name, value)</h3>
|
||||
|
||||
Assigns setting `name` to `value`, where `name` is one of the properties from
|
||||
the [app settings table](#app.settings.table).
|
||||
|
||||
Calling `app.set('foo', true)` for a Boolean property is the same as calling
|
||||
`app.enable('foo')`. Similarly, calling `app.set('foo', false)` for a Boolean
|
||||
property is the same as calling `app.disable('foo')`.
|
||||
|
||||
Retrieve the value of a setting with [`app.get()`](#app.get).
|
||||
|
||||
~~~js
|
||||
app.set('title', 'My Site');
|
||||
app.get('title'); // "My Site"
|
||||
~~~
|
||||
|
||||
<h4 id='app.settings.table'>Application Settings</h4>
|
||||
|
||||
{% include api/{{ page.lang }}/4x/app-settings.md %}
|
||||
228
_includes/api/es/4x/app-settings.md
Normal file
228
_includes/api/es/4x/app-settings.md
Normal file
@@ -0,0 +1,228 @@
|
||||
If `name` is one of the application settings, it affects the behavior of the application. The following table lists application settings.
|
||||
|
||||
<div class="table-scroller">
|
||||
<table class="doctable" border="1">
|
||||
<thead><tr><th id="app-settings-property">Property</th><th>Type</th><th>Value</th><th>Default</th></tr></thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`case sensitive routing`
|
||||
</td>
|
||||
<td>Boolean</td>
|
||||
<td>Enable case sensitivity.</td>
|
||||
<td>Disabled. Treats "/Foo" and "/foo" as the same.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`env`
|
||||
</td>
|
||||
<td>String</td>
|
||||
<td>Environment mode.</td>
|
||||
<td markdown="1">
|
||||
`process.env.NODE_ENV` (`NODE_ENV` environment variable) or "development".
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`etag`
|
||||
</td>
|
||||
<td>Varied</td>
|
||||
<td markdown="1">
|
||||
Set the ETag response header. For possible values, see the [`etag` options table](#etag.options.table).
|
||||
|
||||
[More about the HTTP ETag header](http://en.wikipedia.org/wiki/HTTP_ETag).
|
||||
</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`jsonp callback name`
|
||||
</td>
|
||||
<td>String</td>
|
||||
<td>Specifies the default JSONP callback name.</td>
|
||||
<td markdown="1">
|
||||
`?callback=`
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`json replacer`
|
||||
</td>
|
||||
<td>String</td>
|
||||
<td>JSON replacer callback.</td>
|
||||
<td markdown="1">
|
||||
`null`
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`json spaces`
|
||||
</td>
|
||||
<td>Number</td>
|
||||
<td>When set, sends prettified JSON string indented with the specified amount of spaces.</td>
|
||||
<td>Disabled.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`query parser`
|
||||
</td>
|
||||
<td>String</td>
|
||||
<td markdown="1">
|
||||
The query parser to use, either "simple" or "extended". The simple query parser is based on Node's native query parser, [querystring](http://nodejs.org/api/querystring.html). The extended query parser is based on [qs](https://www.npmjs.org/package/qs).
|
||||
</td>
|
||||
<td>"extended"</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`strict routing`
|
||||
</td>
|
||||
<td>Boolean</td>
|
||||
<td>Enable strict routing.</td>
|
||||
<td>Disabled. Treats "/foo" and "/foo/" as the same by the router.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`subdomain offset`
|
||||
</td>
|
||||
<td>Number</td>
|
||||
<td>The number of dot-separated parts of the host to remove to access subdomain.</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`trust proxy`
|
||||
</td>
|
||||
<td>Varied</td>
|
||||
<td markdown="1">
|
||||
Indicates the app is behind a front-facing proxy, and to use the `X-Forwarded-*` headers to determine the connection and the IP address of the client. NOTE: `X-Forwarded-*` headers are easily spoofed and the detected IP addresses are unreliable.
|
||||
|
||||
`trust proxy` is disabled by default. When enabled, Express attempts to determine the IP address of the client connected through the front-facing proxy, or series of proxies. The `req.ips` property, then, contains an array of IP addresses the client is connected through. To enable it, use the values described in the [`trust proxy` options table](#trust.proxy.options.table).
|
||||
|
||||
The `trust proxy` setting is implemented using the [proxy-addr](https://www.npmjs.org/package/proxy-addr) package. For more information, see its documentation.
|
||||
</td>
|
||||
<td>Disabled.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`views`
|
||||
</td>
|
||||
<td>String or Array</td>
|
||||
<td>A directory or an array of directories for the application's views. If an array, the views are looked up in the order they occur in the array.</td>
|
||||
<td markdown="1">
|
||||
`process.cwd() + '/views'`
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`view cache`
|
||||
</td>
|
||||
<td>Boolean</td>
|
||||
<td>Enables view template compilation caching.</td>
|
||||
<td markdown="1">
|
||||
`true` in production.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`view engine`
|
||||
</td>
|
||||
<td>String</td>
|
||||
<td>The default engine extension to use when omitted.</td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td markdown="1">
|
||||
`x-powered-by`
|
||||
</td>
|
||||
<td>Boolean</td>
|
||||
<td>Enables the "X-Powered-By: Express" HTTP header.</td>
|
||||
<td markdown="1">
|
||||
`true`
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h5 id="trust.proxy.options.table">Options for `trust proxy` setting</h5>
|
||||
|
||||
<table class="doctable" border="1">
|
||||
<thead><tr><th>Type</th><th>Value</th></tr></thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Boolean</td>
|
||||
<td markdown="1">
|
||||
If `true`, the client's IP address is understood as the left-most entry in the `X-Forwarded-*` header.
|
||||
|
||||
If `false`, the app is understood as directly facing the Internet and the client's IP address is derived from `req.connection.remoteAddress`. This is the default setting.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>IP addresses</td>
|
||||
<td markdown="1">
|
||||
An IP address, subnet, or an array of IP addresses, and subnets to trust. The following is the list of pre-configured subnet names.
|
||||
|
||||
* loopback - `127.0.0.1/8`, `::1/128`
|
||||
* linklocal - `169.254.0.0/16`, `fe80::/10`
|
||||
* uniquelocal - `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`, `fc00::/7`
|
||||
|
||||
Set IP addresses in any of the following ways:
|
||||
|
||||
<pre><code class="language-js">app.set('trust proxy', 'loopback') // specify a single subnet
|
||||
app.set('trust proxy', 'loopback, 123.123.123.123') // specify a subnet and an address
|
||||
app.set('trust proxy', 'loopback, linklocal, uniquelocal') // specify multiple subnets as CSV
|
||||
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']) // specify multiple subnets as an array</code></pre>
|
||||
|
||||
When specified, the IP addresses or the subnets are excluded from the address determination process, and the untrusted IP address nearest to the application server is determined as the client's IP address.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Number</td>
|
||||
<td markdown="1">
|
||||
Trust the `n`th hop from the front-facing proxy server as the client.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Function</td>
|
||||
<td markdown="1">
|
||||
Custom trust implementation. Use this only if you know what you are doing.
|
||||
<pre><code class="language-js">app.set('trust proxy', function (ip) {
|
||||
if (ip === '127.0.0.1' || ip === '123.123.123.123') return true; // trusted IPs
|
||||
else return false;
|
||||
})</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h5 id="etag.options.table">Options for `etag` setting</h5>
|
||||
|
||||
<table class="doctable" border="1">
|
||||
<thead><tr><th>Type</th><th>Value</th></tr></thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>Boolean</td>
|
||||
<td markdown="1">
|
||||
`true` enables weak ETag. This is the default setting.<br>
|
||||
`false` disables ETag altogether.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>String</td>
|
||||
<td>
|
||||
If "strong", enables strong ETag.<br>
|
||||
If "weak", enables weak ETag.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Function</td>
|
||||
<td markdown="1">Custom ETag function implementation. Use this only if you know what you are doing.
|
||||
|
||||
<pre><code class="language-js">app.set('etag', function (body, encoding) {
|
||||
return generateHash(body, encoding); // consider the function is defined
|
||||
})</code></pre>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
250
_includes/api/es/4x/app-use.md
Normal file
250
_includes/api/es/4x/app-use.md
Normal file
@@ -0,0 +1,250 @@
|
||||
<h3 id='app.use'>app.use([path,] function [, function...])</h3>
|
||||
|
||||
Mounts the [middleware](/guide/using-middleware.html) `function`(s) at the `path`. If `path` is not specified, it defaults to "/".
|
||||
|
||||
<div class="doc-box doc-notice" markdown="1">
|
||||
A route will match any path, which follows its path immediately with a "<code>/</code>".
|
||||
For example: <code>app.use('/apple', ...)</code> will match <b>/apple</b>, <b>/apple/images</b>,
|
||||
<b>/apple/images/news</b>, and so on.
|
||||
</div>
|
||||
|
||||
Mounting a middleware at a `path` will cause the middleware function to be executed whenever the base of the requested path matches the `path`.
|
||||
|
||||
Since `path` defaults to "/", middleware mounted without a path will be executed for every request to the app.
|
||||
|
||||
~~~js
|
||||
// this middleware will be executed for every request to the app
|
||||
app.use(function (req, res, next) {
|
||||
console.log('Time: %d', Date.now());
|
||||
next();
|
||||
})
|
||||
~~~
|
||||
|
||||
Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.
|
||||
|
||||
~~~js
|
||||
// this middleware will not allow the request to go beyond it
|
||||
app.use(function(req, res, next) {
|
||||
res.send('Hello World');
|
||||
})
|
||||
|
||||
// requests will never reach this route
|
||||
app.get('/', function (req, res) {
|
||||
res.send('Welcome');
|
||||
})
|
||||
~~~
|
||||
|
||||
`path` can be a string representing a path, a path pattern, a regular expression to match paths,
|
||||
or an array of combinations thereof.
|
||||
|
||||
<div class="doc-box doc-notice" markdown="1">
|
||||
The middleware in the below are simple examples.
|
||||
</div>
|
||||
|
||||
<div class="table-scroller">
|
||||
<table class="doctable" border="1">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th> Type </th>
|
||||
<th> Example </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
<tr>
|
||||
<td>Path</td>
|
||||
<td>
|
||||
<pre><code class="language-js">// will match paths starting with /abcd
|
||||
app.use('/abcd', function (req, res, next) {
|
||||
next();
|
||||
})</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Path Pattern</td>
|
||||
<td>
|
||||
<pre><code class="language-js">// will match paths starting with /abcd and /abd
|
||||
app.use('/abc?d', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
// will match paths starting with /abcd, /abbcd, /abbbbbcd and so on
|
||||
app.use('/ab+cd', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
// will match paths starting with /abcd, /abxcd, /abFOOcd, /abbArcd and so on
|
||||
app.use('/ab\*cd', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
// will match paths starting with /ad and /abcd
|
||||
app.use('/a(bc)?d', function (req, res, next) {
|
||||
next();
|
||||
})</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Regular Expression</td>
|
||||
<td>
|
||||
<pre><code class="language-js">// will match paths starting with /abc and /xyz
|
||||
app.use(/\/abc|\/xyz/, function (req, res, next) {
|
||||
next();
|
||||
})</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Array</td>
|
||||
<td>
|
||||
<pre><code class="language-js">// will match paths starting with /abcd, /xyza, /lmn, and /pqr
|
||||
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) {
|
||||
next();
|
||||
})</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
|
||||
`function` can be a middleware function, a series of middleware functions,
|
||||
an array of middleware functions, or a combination of all of them.
|
||||
Since [router](#router) and [app](#application) implement the middleware interface, you can use them
|
||||
as you would any other middleware function.
|
||||
|
||||
<table class="doctable" border="1">
|
||||
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Usage</th>
|
||||
<th>Example</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
<tr>
|
||||
<td>Single Middleware</td>
|
||||
<td>You can define and mount a middleware function locally.
|
||||
<pre><code class="language-js">app.use(function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
</code></pre>
|
||||
A router is valid middleware.
|
||||
|
||||
<pre><code class="language-js">var router = express.Router();
|
||||
router.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
app.use(router);
|
||||
</code></pre>
|
||||
|
||||
An Express app is valid middleware.
|
||||
<pre><code class="language-js">var subApp = express();
|
||||
subApp.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
app.use(subApp);
|
||||
</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Series of Middleware</td>
|
||||
<td>
|
||||
You can specify more than one middleware function at the same mount path.
|
||||
<pre><code class="language-js">var r1 = express.Router();
|
||||
r1.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
var r2 = express.Router();
|
||||
r2.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
app.use(r1, r2);
|
||||
</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Array</td>
|
||||
<td>
|
||||
Use an array to group middleware logically.
|
||||
If you pass an array of middleware as the first or only middleware parameters, then you _must_ specify the mount path.
|
||||
<pre><code class="language-js">var r1 = express.Router();
|
||||
r1.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
var r2 = express.Router();
|
||||
r2.get('/', function (req, res, next) {
|
||||
next();
|
||||
})
|
||||
|
||||
app.use('/', [r1, r2]);
|
||||
</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Combination</td>
|
||||
<td>
|
||||
You can combine all the above ways of mounting middleware.
|
||||
<pre><code class="language-js">function mw1(req, res, next) { next(); }
|
||||
function mw2(req, res, next) { next(); }
|
||||
|
||||
var r1 = express.Router();
|
||||
r1.get('/', function (req, res, next) { next(); });
|
||||
|
||||
var r2 = express.Router();
|
||||
r2.get('/', function (req, res, next) { next(); });
|
||||
|
||||
var subApp = express();
|
||||
subApp.get('/', function (req, res, next) { next(); });
|
||||
|
||||
app.use(mw1, [mw2, r1, r2], subApp);
|
||||
</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
|
||||
Following are some examples of using the [express.static](/guide/using-middleware.html#middleware.built-in)
|
||||
middleware in an Express app.
|
||||
|
||||
Serve static content for the app from the "public" directory in the application directory:
|
||||
|
||||
~~~js
|
||||
// GET /style.css etc
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
~~~
|
||||
|
||||
Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static":
|
||||
|
||||
~~~js
|
||||
// GET /static/style.css etc.
|
||||
app.use('/static', express.static(__dirname + '/public'));
|
||||
~~~
|
||||
|
||||
Disable logging for static content requests by loading the logger middleware after the static middleware:
|
||||
|
||||
~~~js
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(logger());
|
||||
~~~
|
||||
|
||||
Serve static files from multiple directories, but give precedence to "./public" over the others:
|
||||
|
||||
~~~js
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
app.use(express.static(__dirname + '/files'));
|
||||
app.use(express.static(__dirname + '/uploads'));
|
||||
~~~
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user