Mercurial > hg > nginx-site
comparison xml/en/docs/njs/examples.xml @ 2531:9c8a89d3876f
Updated njs examples with js_import and corresponding changes.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 22 Apr 2020 22:55:58 +0100 |
parents | 351a669a576d |
children | 87f34fafa4e8 |
comparison
equal
deleted
inserted
replaced
2530:407c5bd5bffc | 2531:9c8a89d3876f |
---|---|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
8 | 8 |
9 <article name="Examples" | 9 <article name="Examples" |
10 link="/en/docs/njs/examples.html" | 10 link="/en/docs/njs/examples.html" |
11 lang="en" | 11 lang="en" |
12 rev="12"> | 12 rev="13"> |
13 | |
14 <section id="summary"> | |
15 | |
16 <para> | |
17 The examples work since | |
18 <link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>. | |
19 </para> | |
20 | |
21 </section> | |
22 | |
13 | 23 |
14 <section id="helloword" name="Hello World"> | 24 <section id="helloword" name="Hello World"> |
15 | 25 |
16 <para> | 26 <para> |
17 <path>nginx.conf</path>: | 27 <path>nginx.conf</path>: |
19 load_module modules/ngx_http_js_module.so; | 29 load_module modules/ngx_http_js_module.so; |
20 | 30 |
21 events {} | 31 events {} |
22 | 32 |
23 http { | 33 http { |
24 js_include hello_world.js; | 34 js_import http.js; |
25 | 35 js_content http.hello; |
26 server { | 36 } |
27 listen 8000; | 37 |
28 | 38 </example> |
29 location / { | 39 </para> |
30 js_content hello; | 40 |
31 } | 41 <para> |
32 } | 42 <literal>http.js</literal>: |
33 } | |
34 | |
35 </example> | |
36 </para> | |
37 | |
38 <para> | |
39 <literal>hello_world.js</literal>: | |
40 <example> | 43 <example> |
41 function hello(r) { | 44 function hello(r) { |
42 r.return(200, "Hello world!"); | 45 r.return(200, "Hello world!"); |
43 } | 46 } |
47 | |
48 export default {hello}; | |
44 </example> | 49 </example> |
45 </para> | 50 </para> |
46 | 51 |
47 </section> | 52 </section> |
48 | 53 |
50 <section id="urldecode" name="URL Decoding"> | 55 <section id="urldecode" name="URL Decoding"> |
51 | 56 |
52 <para> | 57 <para> |
53 <path>nginx.conf</path>: | 58 <path>nginx.conf</path>: |
54 <example> | 59 <example> |
55 js_include urldecode.js; | 60 js_import http.js; |
56 | 61 |
57 js_set $decoded_foo decoded_foo; | 62 js_set $decoded_foo http.decoded_foo; |
58 </example> | 63 </example> |
59 </para> | 64 </para> |
60 | 65 |
61 <para> | 66 <para> |
62 <path>urldecode.js</path>: | 67 <path>http.js</path>: |
63 <example> | 68 <example> |
64 function decoded_foo(r) { | 69 function decoded_foo(r) { |
65 return decodeURIComponent(r.args.foo); | 70 return decodeURIComponent(r.args.foo); |
66 } | 71 } |
72 | |
73 export default {decoded_foo}; | |
67 </example> | 74 </example> |
68 </para> | 75 </para> |
69 | 76 |
70 </section> | 77 </section> |
71 | 78 |
73 <section id="urlencode" name="URL Encoding"> | 80 <section id="urlencode" name="URL Encoding"> |
74 | 81 |
75 <para> | 82 <para> |
76 <path>nginx.conf</path>: | 83 <path>nginx.conf</path>: |
77 <example> | 84 <example> |
78 js_include urlencode.js; | 85 js_import http.js; |
79 | 86 |
80 js_set $encoded_foo encoded_foo; | 87 js_set $encoded_foo http.encoded_foo; |
81 ... | 88 ... |
82 | 89 |
83 location / { | 90 location / { |
84 proxy_pass http://example.com?foo=$encoded_foo; | 91 proxy_pass http://example.com?foo=$encoded_foo; |
85 } | 92 } |
86 </example> | 93 </example> |
87 </para> | 94 </para> |
88 | 95 |
89 <para> | 96 <para> |
90 <path>urlencode.js</path>: | 97 <path>http.js</path>: |
91 <example> | 98 <example> |
92 function encoded_foo(r) { | 99 function encoded_foo(r) { |
93 return encodeURIComponent('foo & bar?'); | 100 return encodeURIComponent('foo & bar?'); |
94 } | 101 } |
102 | |
103 export default {encoded_foo}; | |
95 </example> | 104 </example> |
96 </para> | 105 </para> |
97 | 106 |
98 </section> | 107 </section> |
99 | 108 |
101 <section id="redirect" name="Internal Redirect"> | 110 <section id="redirect" name="Internal Redirect"> |
102 | 111 |
103 <para> | 112 <para> |
104 <path>nginx.conf</path>: | 113 <path>nginx.conf</path>: |
105 <example> | 114 <example> |
106 js_include redirect.js; | 115 js_import http.js; |
107 | 116 |
108 location /redirect { | 117 location /redirect { |
109 js_content redirect; | 118 js_content http.redirect; |
110 } | 119 } |
111 | 120 |
112 location @named { | 121 location @named { |
113 return 200 named; | 122 return 200 named; |
114 } | 123 } |
115 </example> | 124 </example> |
116 </para> | 125 </para> |
117 | 126 |
118 <para> | 127 <para> |
119 <path>redirect.js</path>: | 128 <path>http.js</path>: |
120 <example> | 129 <example> |
121 function redirect(r) { | 130 function redirect(r) { |
122 r.internalRedirect('@named'); | 131 r.internalRedirect('@named'); |
123 } | 132 } |
133 | |
134 export default {redirect}; | |
124 </example> | 135 </example> |
125 </para> | 136 </para> |
126 | 137 |
127 </section> | 138 </section> |
128 | 139 |
130 <section id="fast_response" name="Returning Fastest Response from Proxy"> | 141 <section id="fast_response" name="Returning Fastest Response from Proxy"> |
131 | 142 |
132 <para> | 143 <para> |
133 <path>nginx.conf</path>: | 144 <path>nginx.conf</path>: |
134 <example> | 145 <example> |
135 js_include fastresponse.js; | 146 js_import http.js; |
136 | 147 |
137 location /start { | 148 location /start { |
138 js_content content; | 149 js_content http.content; |
139 } | 150 } |
140 | 151 |
141 location /foo { | 152 location /foo { |
142 proxy_pass http://backend1; | 153 proxy_pass http://backend1; |
143 } | 154 } |
147 } | 158 } |
148 </example> | 159 </example> |
149 </para> | 160 </para> |
150 | 161 |
151 <para> | 162 <para> |
152 <path>fastresponse.js</path>: | 163 <path>http.js</path>: |
153 <example> | 164 <example> |
154 function content(r) { | 165 function content(r) { |
155 var n = 0; | 166 var n = 0; |
156 | 167 |
157 function done(res) { | 168 function done(res) { |
161 } | 172 } |
162 | 173 |
163 r.subrequest('/foo', r.variables.args, done); | 174 r.subrequest('/foo', r.variables.args, done); |
164 r.subrequest('/bar', r.variables.args, done); | 175 r.subrequest('/bar', r.variables.args, done); |
165 } | 176 } |
177 | |
178 export default {content}; | |
166 </example> | 179 </example> |
167 </para> | 180 </para> |
168 | 181 |
169 </section> | 182 </section> |
170 | 183 |
172 <section id="jwt" name="Creating HS JWT"> | 185 <section id="jwt" name="Creating HS JWT"> |
173 | 186 |
174 <para> | 187 <para> |
175 <path>nginx.conf</path>: | 188 <path>nginx.conf</path>: |
176 <example> | 189 <example> |
177 js_include hs_jwt.js; | 190 js_import http.js; |
178 | 191 |
179 js_set $jwt jwt; | 192 js_set $jwt http.jwt; |
180 </example> | 193 </example> |
181 </para> | 194 </para> |
182 | 195 |
183 <para> | 196 <para> |
184 <path>hs_jwt.js</path>: | 197 <path>http.js</path>: |
185 <example> | 198 <example> |
186 function generate_hs256_jwt(claims, key, valid) { | 199 function generate_hs256_jwt(claims, key, valid) { |
187 var header = { typ: "JWT", alg: "HS256" }; | 200 var header = { typ: "JWT", alg: "HS256" }; |
188 var claims = Object.assign(claims, {exp: Math.floor(Date.now()/1000) + valid}); | 201 var claims = Object.assign(claims, {exp: Math.floor(Date.now()/1000) + valid}); |
189 | 202 |
206 zyx: false | 219 zyx: false |
207 }; | 220 }; |
208 | 221 |
209 return generate_hs256_jwt(claims, 'foo', 600); | 222 return generate_hs256_jwt(claims, 'foo', 600); |
210 } | 223 } |
224 | |
225 export default {jwt}; | |
211 </example> | 226 </example> |
212 </para> | 227 </para> |
213 | 228 |
214 </section> | 229 </section> |
215 | 230 |
217 <section id="subrequest" name="Accessing API from a Subrequest"> | 232 <section id="subrequest" name="Accessing API from a Subrequest"> |
218 | 233 |
219 <para> | 234 <para> |
220 <path>nginx.conf</path>: | 235 <path>nginx.conf</path>: |
221 <example> | 236 <example> |
222 js_include subrequest.js; | 237 js_import http.js; |
223 | 238 |
224 keyval_zone zone=foo:10m; | 239 keyval_zone zone=foo:10m; |
225 ... | 240 ... |
226 | 241 |
227 location /keyval { | 242 location /keyval { |
228 js_content set_keyval; | 243 js_content http.set_keyval; |
229 } | 244 } |
230 | 245 |
231 location /version { | 246 location /version { |
232 js_content version; | 247 js_content http.version; |
233 } | 248 } |
234 | 249 |
235 location /api { | 250 location /api { |
236 api write=on; | 251 api write=on; |
237 } | 252 } |
238 </example> | 253 </example> |
239 </para> | 254 </para> |
240 | 255 |
241 <para> | 256 <para> |
242 <path>subrequest.js</path>: | 257 <path>http.js</path>: |
243 <example> | 258 <example> |
244 function set_keyval(r) { | 259 function set_keyval(r) { |
245 r.subrequest('/api/5/http/keyvals/foo', | 260 r.subrequest('/api/5/http/keyvals/foo', |
246 { method: 'POST', | 261 { method: 'POST', |
247 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, | 262 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, |
264 | 279 |
265 var json = JSON.parse(res.responseBody); | 280 var json = JSON.parse(res.responseBody); |
266 r.return(200, json.version); | 281 r.return(200, json.version); |
267 }); | 282 }); |
268 } | 283 } |
284 | |
285 export default {set_keyval, version}; | |
269 </example> | 286 </example> |
270 </para> | 287 </para> |
271 | 288 |
272 </section> | 289 </section> |
273 | 290 |
275 <section id="secure_link" name="Creating secure_link Hash"> | 292 <section id="secure_link" name="Creating secure_link Hash"> |
276 | 293 |
277 <para> | 294 <para> |
278 <path>nginx.conf</path>: | 295 <path>nginx.conf</path>: |
279 <example> | 296 <example> |
280 js_include hash.js; | 297 js_import http.js; |
281 | 298 |
282 js_set $new_foo create_secure_link; | 299 js_set $new_foo http.create_secure_link; |
283 ... | 300 ... |
284 | 301 |
285 location / { | 302 location / { |
286 secure_link $cookie_foo; | 303 secure_link $cookie_foo; |
287 secure_link_md5 "$uri mykey"; | 304 secure_link_md5 "$uri mykey"; |
294 } | 311 } |
295 </example> | 312 </example> |
296 </para> | 313 </para> |
297 | 314 |
298 <para> | 315 <para> |
299 <path>hash.js</path>: | 316 <path>http.js</path>: |
300 <example> | 317 <example> |
301 function create_secure_link(r) { | 318 function create_secure_link(r) { |
302 return require('crypto').createHash('md5') | 319 return require('crypto').createHash('md5') |
303 .update(r.uri).update(" mykey") | 320 .update(r.uri).update(" mykey") |
304 .digest('base64url'); | 321 .digest('base64url'); |
305 } | 322 } |
323 | |
324 export default {create_secure_link}; | |
306 </example> | 325 </example> |
307 </para> | 326 </para> |
308 | 327 |
309 </section> | 328 </section> |
310 | 329 |
312 <section id="requests" name="Logging the Number of Requests Per Client"> | 331 <section id="requests" name="Logging the Number of Requests Per Client"> |
313 | 332 |
314 <para> | 333 <para> |
315 <path>nginx.conf</path>: | 334 <path>nginx.conf</path>: |
316 <example> | 335 <example> |
317 js_include js_requests.js; | 336 js_import http.js; |
318 | 337 |
319 js_set $num_requests num_requests; | 338 js_set $num_requests http.num_requests; |
320 | 339 |
321 keyval_zone zone=foo:10m; | 340 keyval_zone zone=foo:10m; |
322 | 341 |
323 keyval $remote_addr $foo zone=foo; | 342 keyval $remote_addr $foo zone=foo; |
324 | 343 |
334 } | 353 } |
335 </example> | 354 </example> |
336 </para> | 355 </para> |
337 | 356 |
338 <para> | 357 <para> |
339 <path>js_requests.js</path>: | 358 <path>http.js</path>: |
340 <example> | 359 <example> |
341 function num_requests(r) | 360 function num_requests(r) |
342 { | 361 { |
343 var n = r.variables.foo; | 362 var n = r.variables.foo; |
344 n = n ? Number(n) + 1 : 1; | 363 n = n ? Number(n) + 1 : 1; |
345 r.variables.foo = n; | 364 r.variables.foo = n; |
346 return n; | 365 return n; |
347 } | 366 } |
367 | |
368 export default {num_requests}; | |
348 </example> | 369 </example> |
349 <note> | 370 <note> |
350 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and | 371 The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and |
351 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives | 372 <link doc="../http/ngx_http_keyval_module.xml" id="keyval_zone"/> directives |
352 are available as part of our | 373 are available as part of our |
358 | 379 |
359 | 380 |
360 <section id="subrequests_chaining" name="Subrequests Chaining"> | 381 <section id="subrequests_chaining" name="Subrequests Chaining"> |
361 | 382 |
362 <para> | 383 <para> |
363 The example works since | 384 <path>nginx.conf</path>: |
364 <link doc="changes.xml" id="njs0.3.8">0.3.8</link>. | 385 <example> |
365 </para> | 386 js_import http.js; |
366 | |
367 <para> | |
368 <path>nginx.conf</path>: | |
369 <example> | |
370 js_include subrequests_chaining.js; | |
371 | 387 |
372 location /start { | 388 location /start { |
373 js_content content; | 389 js_content http.content; |
374 } | 390 } |
375 | 391 |
376 location /auth { | 392 location /auth { |
377 proxy_pass http://auth_backend; | 393 proxy_pass http://auth_backend; |
378 } | 394 } |
382 } | 398 } |
383 </example> | 399 </example> |
384 </para> | 400 </para> |
385 | 401 |
386 <para> | 402 <para> |
387 <path>subrequests_chaining.js</path>: | 403 <path>http.js</path>: |
388 <example> | 404 <example> |
389 function content(r) { | 405 function content(r) { |
390 r.subrequest('/auth') | 406 r.subrequest('/auth') |
391 .then(reply => JSON.parse(reply.responseBody)) | 407 .then(reply => JSON.parse(reply.responseBody)) |
392 .then(response => { | 408 .then(response => { |
399 r.subrequest('/backend', `token=${token}`) | 415 r.subrequest('/backend', `token=${token}`) |
400 .then(reply => r.return(reply.status, reply.responseBody)); | 416 .then(reply => r.return(reply.status, reply.responseBody)); |
401 }) | 417 }) |
402 .catch(_ => r.return(500)); | 418 .catch(_ => r.return(500)); |
403 } | 419 } |
420 | |
421 export default {content}; | |
404 </example> | 422 </example> |
405 </para> | 423 </para> |
406 | 424 |
407 </section> | 425 </section> |
408 | 426 |