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