OSDN Git Service

Regular updates
[twpd/master.git] / express.md
1 ---
2 title: Express.js
3 category: JavaScript libraries
4 ---
5
6 ### Settings
7
8 ```js
9 app.set('x', 'yyy')
10 app.get('x') //=> 'yyy'
11
12 app.enable('trust proxy')
13 app.disable('trust proxy')
14
15 app.enabled('trust proxy') //=> true
16 ```
17
18 ### Env
19
20 ```js
21 app.get('env')
22 ```
23
24 ### Config
25
26 ```js
27 app.configure('production', function() {
28   app.set...
29 })
30 ```
31
32 ### Wares
33
34 ```js
35 app.use(express.static(__dirname + '/public'))
36 app.use(express.logger())
37 ```
38
39 ### Helpers
40
41 ```js
42 app.locals({
43   title: "MyApp",
44 })
45 ```
46
47 ## Request & response
48
49 ### Request
50
51 ```js
52 // GET  /user/tj
53 req.path         //=> "/user/tj"
54 req.url          //=> "/user/tj"
55 req.xhr          //=> true|false
56 req.method       //=> "GET"
57 req.params
58 req.params.name  //=> "tj"
59 req.params[0]
60 ```
61
62 ```js
63 // GET /search?q=tobi+ferret
64 req.query.q // => "tobi ferret"
65 ```
66
67 ```js
68 req.cookies
69 ```
70
71 ```js
72 req.accepted
73 // [ { value: 'application/json', quality: 1, type: 'application', subtype: 'json' },
74 //   { value: 'text/html', quality: 0.5, type: 'text',subtype: 'html' } ]
75 ```
76
77 ```js
78 req.is('html')
79 req.is('text/html')
80 ```
81
82 ```js
83 req.headers
84 req.headers['host']
85 req.headers['user-agent']
86 req.headers['accept-encoding']
87 req.headers['accept-language']
88 ```
89
90 ### Response
91
92 ```js
93 res.redirect('/')
94 res.redirect(301, '/')
95 ```
96
97 ```js
98 res.set('Content-Type', 'text/html')
99 ```
100
101 ```js
102 res.send('hi')
103 res.send(200, 'hi')
104 ```
105
106 ```js
107 res.json({ a: 2 })
108 ```