express: Update formatting
This commit is contained in:
parent
b7f35db21d
commit
a0f24f42d9
118
express.md
118
express.md
|
@ -5,74 +5,104 @@ category: JavaScript libraries
|
||||||
|
|
||||||
### Settings
|
### Settings
|
||||||
|
|
||||||
app.set('x', 'yyy')
|
```js
|
||||||
app.get('x') //=> 'yyy'
|
app.set('x', 'yyy')
|
||||||
|
app.get('x') //=> 'yyy'
|
||||||
|
|
||||||
app.enable('trust proxy')
|
app.enable('trust proxy')
|
||||||
app.disable('trust proxy')
|
app.disable('trust proxy')
|
||||||
|
|
||||||
app.enabled('trust proxy') //=> true
|
app.enabled('trust proxy') //=> true
|
||||||
|
```
|
||||||
|
|
||||||
### Env
|
### Env
|
||||||
|
|
||||||
app.get('env')
|
```js
|
||||||
|
app.get('env')
|
||||||
|
```
|
||||||
|
|
||||||
### Config
|
### Config
|
||||||
|
|
||||||
app.configure('production', function() {
|
```js
|
||||||
app.set...
|
app.configure('production', function() {
|
||||||
});
|
app.set...
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
### Wares
|
### Wares
|
||||||
|
|
||||||
app.use(express.static(__dirname + '/public'));
|
```js
|
||||||
app.use(express.logger());
|
app.use(express.static(__dirname + '/public'))
|
||||||
|
app.use(express.logger())
|
||||||
|
```
|
||||||
|
|
||||||
### Helpers
|
### Helpers
|
||||||
|
|
||||||
app.locals({
|
```js
|
||||||
title: "MyApp",
|
app.locals({
|
||||||
});
|
title: "MyApp",
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Request & response
|
||||||
|
|
||||||
### Request
|
### Request
|
||||||
|
|
||||||
|
```js
|
||||||
|
// GET /user/tj
|
||||||
|
req.path //=> "/user/tj"
|
||||||
|
req.url //=> "/user/tj"
|
||||||
|
req.xhr //=> true|false
|
||||||
|
req.method //=> "GET"
|
||||||
|
req.params
|
||||||
|
req.params.name //=> "tj"
|
||||||
|
req.params[0]
|
||||||
|
```
|
||||||
|
|
||||||
// GET /user/tj
|
```js
|
||||||
req.path //=> "/user/tj"
|
// GET /search?q=tobi+ferret
|
||||||
req.url //=> "/user/tj"
|
req.query.q // => "tobi ferret"
|
||||||
req.xhr //=> true|false
|
```
|
||||||
req.method //=> "GET"
|
|
||||||
req.params
|
|
||||||
req.params.name //=> "tj"
|
|
||||||
req.params[0]
|
|
||||||
|
|
||||||
// GET /search?q=tobi+ferret
|
```js
|
||||||
req.query.q // => "tobi ferret"
|
req.cookies
|
||||||
|
```
|
||||||
|
|
||||||
req.cookies
|
```js
|
||||||
|
req.accepted
|
||||||
|
// [ { value: 'application/json', quality: 1, type: 'application', subtype: 'json' },
|
||||||
|
// { value: 'text/html', quality: 0.5, type: 'text',subtype: 'html' } ]
|
||||||
|
```
|
||||||
|
|
||||||
req.accepted
|
```js
|
||||||
[ { value: 'application/json', quality: 1, type: 'application', subtype: 'json' },
|
req.is('html')
|
||||||
{ value: 'text/html', quality: 0.5, type: 'text',subtype: 'html' } ]
|
req.is('text/html')
|
||||||
|
```
|
||||||
|
|
||||||
req.is('html')
|
```js
|
||||||
req.is('text/html')
|
req.headers
|
||||||
|
req.headers['host']
|
||||||
|
req.headers['user-agent']
|
||||||
|
req.headers['accept-encoding']
|
||||||
|
req.headers['accept-language']
|
||||||
|
```
|
||||||
|
|
||||||
req.headers
|
### Response
|
||||||
req.headers['host']
|
|
||||||
req.headers['user-agent']
|
|
||||||
req.headers['accept-encoding']
|
|
||||||
req.headers['accept-language']
|
|
||||||
|
|
||||||
|
```js
|
||||||
|
res.redirect('/')
|
||||||
|
res.redirect(301, '/')
|
||||||
|
```
|
||||||
|
|
||||||
## Response
|
```js
|
||||||
|
res.set('Content-Type', 'text/html')
|
||||||
|
```
|
||||||
|
|
||||||
res.redirect('/')
|
```js
|
||||||
res.redirect(301, '/')
|
res.send('hi')
|
||||||
|
res.send(200, 'hi')
|
||||||
|
```
|
||||||
|
|
||||||
res.set('Content-Type', 'text/html')
|
```js
|
||||||
|
res.json({ a: 2 })
|
||||||
res.send('hi')
|
```
|
||||||
res.send(200, 'hi')
|
|
||||||
|
|
||||||
res.json({ a: 2 })
|
|
||||||
|
|
Loading…
Reference in New Issue