Mercurial > hg > nginx-site
comparison xml/en/docs/njs/njs_api.xml @ 2178:cb431c861670
Added njs examples.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 05 Jun 2018 18:23:50 +0300 |
parents | 79297494d291 |
children | 7865ca0da0ab |
comparison
equal
deleted
inserted
replaced
2177:79297494d291 | 2178:cb431c861670 |
---|---|
537 </section> | 537 </section> |
538 | 538 |
539 </section> | 539 </section> |
540 | 540 |
541 | 541 |
542 <section id="example" name="Examples"> | |
543 | |
544 | |
545 <section id="example_urldecode" name="URL Decoding"> | |
546 | |
547 <para> | |
548 <example> | |
549 js_include urldecode.js; | |
550 | |
551 js_set $decoded_foo decoded_foo; | |
552 </example> | |
553 </para> | |
554 | |
555 <para> | |
556 The <path>urldecode.js</path> file: | |
557 <example> | |
558 function decoded_foo(req, res) { | |
559 return decodeURIComponent(req.args.foo); | |
560 } | |
561 </example> | |
562 </para> | |
563 | |
564 </section> | |
565 | |
566 | |
567 <section id="example_urlencode" name="URL Encoding"> | |
568 | |
569 <para> | |
570 <example> | |
571 js_include urlencode.js; | |
572 | |
573 js_set $encoded_foo encoded_foo; | |
574 ... | |
575 | |
576 location / { | |
577 proxy_pass http://example.com?foo=$encoded_foo; | |
578 } | |
579 </example> | |
580 </para> | |
581 | |
582 <para> | |
583 The <path>urlencode.js</path> file: | |
584 <example> | |
585 function encoded_foo(req, res) { | |
586 return encodeURIComponent('foo & bar?'); | |
587 } | |
588 </example> | |
589 </para> | |
590 | |
591 </section> | |
592 | |
593 | |
594 <section id="example_fast_response" name="Returning Fastest Response from Proxy"> | |
595 | |
596 <para> | |
597 <example> | |
598 js_include fastresponse.js; | |
599 | |
600 location /start { | |
601 js_content content; | |
602 } | |
603 | |
604 location /foo { | |
605 proxy_pass http://backend1; | |
606 } | |
607 | |
608 location /bar { | |
609 proxy_pass http://backend2; | |
610 } | |
611 </example> | |
612 </para> | |
613 | |
614 <para> | |
615 The <path>fastresponse.js</path> file: | |
616 <example> | |
617 function content(req, res) { | |
618 var n = 0; | |
619 | |
620 function done(reply) { | |
621 if (n++ == 0) { | |
622 res.return(reply.status, reply.body); | |
623 } | |
624 } | |
625 | |
626 req.subrequest('/foo', req.variables.args, done); | |
627 req.subrequest('/bar', req.variables.args, done); | |
628 } | |
629 </example> | |
630 </para> | |
631 | |
632 </section> | |
633 | |
634 | |
635 <section id="example_jwt" name="Creating HS JWT"> | |
636 | |
637 <para> | |
638 <example> | |
639 js_include hs_jwt.js; | |
640 | |
641 js_set $jwt jwt; | |
642 </example> | |
643 </para> | |
644 | |
645 <para> | |
646 The <path>hs_jwt.js</path> file: | |
647 <example> | |
648 function create_hs256_jwt(claims, key, valid) { | |
649 var header = { "typ" : "JWT", "alg" : "HS256", "exp" : Date.now() + valid }; | |
650 | |
651 var s = JSON.stringify(header).toBytes().toString('base64url') + '.' | |
652 + JSON.stringify(claims).toBytes().toString('base64url'); | |
653 | |
654 var h = require('crypto').createHmac('sha256', key); | |
655 | |
656 return s + '.' + h.update(s).digest().toString('base64url'); | |
657 } | |
658 | |
659 function jwt(req, res) { | |
660 var claims = { | |
661 "iss" : "nginx", | |
662 "sub" : "alice", | |
663 "foo" : 123, | |
664 "bar" : "qq", | |
665 "zyx" : false | |
666 }; | |
667 | |
668 return create_hs256_jwt(claims, 'foo', 600); | |
669 } | |
670 </example> | |
671 </para> | |
672 | |
673 </section> | |
674 | |
675 | |
676 <section id="example_subrequest" name="Accessing API from a Subrequest"> | |
677 | |
678 <para> | |
679 <example> | |
680 js_include subrequest.js; | |
681 | |
682 keyval_zone zone=foo:10m; | |
683 ... | |
684 | |
685 location /keyval { | |
686 js_content set_keyval; | |
687 } | |
688 | |
689 location /version { | |
690 js_content version; | |
691 } | |
692 | |
693 location /api { | |
694 api write=on; | |
695 } | |
696 </example> | |
697 </para> | |
698 | |
699 <para> | |
700 The <path>subrequest.js</path> file: | |
701 <example> | |
702 function set_keyval(req, res) { | |
703 req.subrequest('/api/3/http/keyvals/foo', | |
704 { method: 'POST', | |
705 body: JSON.stringify({ foo: 789, bar: "ss dd 00" })}, | |
706 | |
707 function(reply) { | |
708 if (reply.status >= 300) { | |
709 res.return(reply.status, reply.body); | |
710 return; | |
711 } | |
712 res.return(500); | |
713 }); | |
714 } | |
715 function version(req, res) { | |
716 req.subrequest('/api/3/nginx', { method: 'GET' }, function(reply) { | |
717 if (reply.status != 200) { | |
718 res.return(reply.status); | |
719 return; | |
720 } | |
721 | |
722 var json = JSON.parse(reply.body); | |
723 res.return(200, json.version); | |
724 }); | |
725 } | |
726 </example> | |
727 </para> | |
728 | |
729 </section> | |
730 | |
731 | |
732 <section id="example_secure_link" name="Creating secure_link Hash"> | |
733 | |
734 <para> | |
735 <example> | |
736 js_include hash.js; | |
737 | |
738 js_set $new_foo create_secure_link; | |
739 ... | |
740 | |
741 location / { | |
742 secure_link $cookie_foo; | |
743 secure_link_md5 "$uri mykey"; | |
744 ... | |
745 } | |
746 | |
747 location @login { | |
748 add_header Set-Cookie "foo=$new_foo; Max-Age=60"; | |
749 return 302 /; | |
750 } | |
751 </example> | |
752 </para> | |
753 | |
754 <para> | |
755 The <path>hash.js</path> file: | |
756 <example> | |
757 function create_secure_link(req, res) { | |
758 return require('crypto').createHash('md5') | |
759 .update(req.uri).update(" mykey") | |
760 .digest('base64url'); | |
761 } | |
762 </example> | |
763 </para> | |
764 | |
765 </section> | |
766 | |
767 </section> | |
768 | |
542 </article> | 769 </article> |