comparison xml/ru/docs/njs/reference.xml @ 2290:65c6c1ee3c78

Removed obsolete "Examples" section from Russian njs reference.
author Yaroslav Zhuravlev <yar@nginx.com>
date Tue, 27 Nov 2018 19:04:53 +0300
parents 32ba43abf9cd
children a1d0238ffb61
comparison
equal deleted inserted replaced
2289:54dbe105fe77 2290:65c6c1ee3c78
290 290
291 </section> 291 </section>
292 292
293 </section> 293 </section>
294 294
295
296 <section id="example" name="Примеры">
297
298
299 <section id="example_urldecode" name="Декодирование URL">
300
301 <para>
302 <example>
303 js_include urldecode.js;
304
305 js_set $decoded_foo decoded_foo;
306 </example>
307 </para>
308
309 <para>
310 Файл <path>urldecode.js</path>:
311 <example>
312 function decoded_foo(r) {
313 return decodeURIComponent(r.args.foo);
314 }
315 </example>
316 </para>
317
318 </section>
319
320
321 <section id="example_urlencode" name="Кодирование URL">
322
323 <para>
324 <example>
325 js_include urlencode.js;
326
327 js_set $encoded_foo encoded_foo;
328 ...
329
330 location / {
331 proxy_pass http://example.com?foo=$encoded_foo;
332 }
333 </example>
334 </para>
335
336 <para>
337 Файл <path>urlencode.js</path>:
338 <example>
339 function encoded_foo(r) {
340 return encodeURIComponent('foo &amp; bar?');
341 }
342 </example>
343 </para>
344
345 </section>
346
347
348 <section id="example_fast_response" name="Возврат самого быстрого ответа от прокси">
349
350 <para>
351 <example>
352 js_include fastresponse.js;
353
354 location /start {
355 js_content content;
356 }
357
358 location /foo {
359 proxy_pass http://backend1;
360 }
361
362 location /bar {
363 proxy_pass http://backend2;
364 }
365 </example>
366 </para>
367
368 <para>
369 Файл <path>fastresponse.js</path>:
370 <example>
371 function content(r) {
372 var n = 0;
373
374 function done(res) {
375 if (n++ == 0) {
376 r.return(res.status, res.responseBody);
377 }
378 }
379
380 r.subrequest('/foo', r.variables.args, done);
381 r.subrequest('/bar', r.variables.args, done);TBD
382 }
383 </example>
384 </para>
385
386 </section>
387
388
389 <section id="example_jwt" name="Создание HS JWT">
390
391 <para>
392 <example>
393 js_include hs_jwt.js;
394
395 js_set $jwt jwt;
396 </example>
397 </para>
398
399 <para>
400 Файл <path>hs_jwt.js</path>:
401 <example>
402 function create_hs256_jwt(claims, key, valid) {
403 var header = { "typ" : "JWT", "alg" : "HS256", "exp" : Date.now() + valid };
404
405 var s = JSON.stringify(header).toBytes().toString('base64url') + '.'
406 + JSON.stringify(claims).toBytes().toString('base64url');
407
408 var h = require('crypto').createHmac('sha256', key);
409
410 return s + '.' + h.update(s).digest().toString('base64url');
411 }
412
413 function jwt(r) {
414 var claims = {
415 "iss" : "nginx",
416 "sub" : "alice",
417 "foo" : 123,
418 "bar" : "qq",
419 "zyx" : false
420 };
421
422 return create_hs256_jwt(claims, 'foo', 600);
423 }
424 </example>
425 </para>
426
427 </section>
428
429
430 <section id="example_subrequest" name="Доступ к API при помощи подзапроса">
431
432 <para>
433 <example>
434 js_include subrequest.js;
435
436 keyval_zone zone=foo:10m;
437 ...
438
439 location /keyval {
440 js_content set_keyval;
441 }
442
443 location /version {
444 js_content version;
445 }
446
447 location /api {
448 api write=on;
449 }
450 </example>
451 </para>
452
453 <para>
454 Файл <path>subrequest.js</path>:
455 <example>
456 function set_keyval(r) {
457 r.subrequest('/api/3/http/keyvals/foo',
458 { method: 'POST',
459 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })},
460
461 function(res) {
462 if (res.status >= 300) {
463 r.return(res.status, res.responseBody);
464 return;
465 }
466 r.return(500);
467 });
468 }
469
470 function version(r) {
471 r.subrequest('/api/3/nginx', { method: 'GET' }, function(res) {
472 if (res.status != 200) {
473 r.return(res.status);
474 return;
475 }
476
477 var json = JSON.parse(res.responseBody);
478 r.return(200, json.version);
479 });
480 }
481 </example>
482 </para>
483
484 </section>
485
486
487 <section id="example_secure_link" name="Создание хэша secure_link">
488
489 <para>
490 <example>
491 js_include hash.js;
492
493 js_set $new_foo create_secure_link;
494 ...
495
496 location / {
497 secure_link $cookie_foo;
498 secure_link_md5 "$uri mykey";
499 ...
500 }
501
502 location @login {
503 add_header Set-Cookie "foo=$new_foo; Max-Age=60";
504 return 302 /;
505 }
506 </example>
507 </para>
508
509 <para>
510 Файл <path>hash.js</path>:
511 <example>
512 function create_secure_link(r) {
513 return require('crypto').createHash('md5')
514 .update(r.uri).update(" mykey")
515 .digest('base64url');
516 }
517 </example>
518 </para>
519
520 </section>
521
522 </section>
523
524 </article> 295 </article>
525 296