Mercurial > hg > nginx-site
diff 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 |
line wrap: on
line diff
--- a/xml/en/docs/njs/examples.xml Wed Apr 22 16:54:11 2020 +0100 +++ b/xml/en/docs/njs/examples.xml Wed Apr 22 22:55:58 2020 +0100 @@ -9,7 +9,17 @@ <article name="Examples" link="/en/docs/njs/examples.html" lang="en" - rev="12"> + rev="13"> + +<section id="summary"> + +<para> +The examples work since +<link doc="../njs/changes.xml" id="njs0.4.0">0.4.0</link>. +</para> + +</section> + <section id="helloword" name="Hello World"> @@ -21,26 +31,21 @@ events {} http { - js_include hello_world.js; - - server { - listen 8000; - - location / { - js_content hello; - } - } + js_import http.js; + js_content http.hello; } </example> </para> <para> -<literal>hello_world.js</literal>: +<literal>http.js</literal>: <example> function hello(r) { r.return(200, "Hello world!"); } + +export default {hello}; </example> </para> @@ -52,18 +57,20 @@ <para> <path>nginx.conf</path>: <example> -js_include urldecode.js; +js_import http.js; -js_set $decoded_foo decoded_foo; +js_set $decoded_foo http.decoded_foo; </example> </para> <para> -<path>urldecode.js</path>: +<path>http.js</path>: <example> function decoded_foo(r) { return decodeURIComponent(r.args.foo); } + +export default {decoded_foo}; </example> </para> @@ -75,9 +82,9 @@ <para> <path>nginx.conf</path>: <example> -js_include urlencode.js; +js_import http.js; -js_set $encoded_foo encoded_foo; +js_set $encoded_foo http.encoded_foo; ... location / { @@ -87,11 +94,13 @@ </para> <para> -<path>urlencode.js</path>: +<path>http.js</path>: <example> function encoded_foo(r) { return encodeURIComponent('foo & bar?'); } + +export default {encoded_foo}; </example> </para> @@ -103,10 +112,10 @@ <para> <path>nginx.conf</path>: <example> -js_include redirect.js; +js_import http.js; location /redirect { - js_content redirect; + js_content http.redirect; } location @named { @@ -116,11 +125,13 @@ </para> <para> -<path>redirect.js</path>: +<path>http.js</path>: <example> function redirect(r) { r.internalRedirect('@named'); } + +export default {redirect}; </example> </para> @@ -132,10 +143,10 @@ <para> <path>nginx.conf</path>: <example> -js_include fastresponse.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /foo { @@ -149,7 +160,7 @@ </para> <para> -<path>fastresponse.js</path>: +<path>http.js</path>: <example> function content(r) { var n = 0; @@ -163,6 +174,8 @@ r.subrequest('/foo', r.variables.args, done); r.subrequest('/bar', r.variables.args, done); } + +export default {content}; </example> </para> @@ -174,14 +187,14 @@ <para> <path>nginx.conf</path>: <example> -js_include hs_jwt.js; +js_import http.js; -js_set $jwt jwt; +js_set $jwt http.jwt; </example> </para> <para> -<path>hs_jwt.js</path>: +<path>http.js</path>: <example> function generate_hs256_jwt(claims, key, valid) { var header = { typ: "JWT", alg: "HS256" }; @@ -208,6 +221,8 @@ return generate_hs256_jwt(claims, 'foo', 600); } + +export default {jwt}; </example> </para> @@ -219,17 +234,17 @@ <para> <path>nginx.conf</path>: <example> -js_include subrequest.js; +js_import http.js; keyval_zone zone=foo:10m; ... location /keyval { - js_content set_keyval; + js_content http.set_keyval; } location /version { - js_content version; + js_content http.version; } location /api { @@ -239,7 +254,7 @@ </para> <para> -<path>subrequest.js</path>: +<path>http.js</path>: <example> function set_keyval(r) { r.subrequest('/api/5/http/keyvals/foo', @@ -266,6 +281,8 @@ r.return(200, json.version); }); } + +export default {set_keyval, version}; </example> </para> @@ -277,9 +294,9 @@ <para> <path>nginx.conf</path>: <example> -js_include hash.js; +js_import http.js; -js_set $new_foo create_secure_link; +js_set $new_foo http.create_secure_link; ... location / { @@ -296,13 +313,15 @@ </para> <para> -<path>hash.js</path>: +<path>http.js</path>: <example> function create_secure_link(r) { return require('crypto').createHash('md5') .update(r.uri).update(" mykey") .digest('base64url'); } + +export default {create_secure_link}; </example> </para> @@ -314,9 +333,9 @@ <para> <path>nginx.conf</path>: <example> -js_include js_requests.js; +js_import http.js; -js_set $num_requests num_requests; +js_set $num_requests http.num_requests; keyval_zone zone=foo:10m; @@ -336,7 +355,7 @@ </para> <para> -<path>js_requests.js</path>: +<path>http.js</path>: <example> function num_requests(r) { @@ -345,6 +364,8 @@ r.variables.foo = n; return n; } + +export default {num_requests}; </example> <note> The <link doc="../http/ngx_http_keyval_module.xml" id="keyval"/> and @@ -360,17 +381,12 @@ <section id="subrequests_chaining" name="Subrequests Chaining"> <para> -The example works since -<link doc="changes.xml" id="njs0.3.8">0.3.8</link>. -</para> - -<para> <path>nginx.conf</path>: <example> -js_include subrequests_chaining.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /auth { @@ -384,7 +400,7 @@ </para> <para> -<path>subrequests_chaining.js</path>: +<path>http.js</path>: <example> function content(r) { r.subrequest('/auth') @@ -401,6 +417,8 @@ }) .catch(_ => r.return(500)); } + +export default {content}; </example> </para>