# HG changeset patch # User Yaroslav Zhuravlev # Date 1587592558 -3600 # Node ID 9c8a89d3876fd6146954e177e95b0fb394a78952 # Parent 407c5bd5bffc3df94470f66bf78cc6acf6e1dc46 Updated njs examples with js_import and corresponding changes. diff --git a/xml/en/docs/njs/examples.xml b/xml/en/docs/njs/examples.xml --- a/xml/en/docs/njs/examples.xml +++ b/xml/en/docs/njs/examples.xml @@ -9,7 +9,17 @@
+ rev="13"> + +
+ + +The examples work since +0.4.0. + + +
+
@@ -21,26 +31,21 @@ load_module modules/ngx_http_js_module.s events {} http { - js_include hello_world.js; - - server { - listen 8000; - - location / { - js_content hello; - } - } + js_import http.js; + js_content http.hello; } -hello_world.js: +http.js: function hello(r) { r.return(200, "Hello world!"); } + +export default {hello}; @@ -52,18 +57,20 @@ function hello(r) { nginx.conf: -js_include urldecode.js; +js_import http.js; -js_set $decoded_foo decoded_foo; +js_set $decoded_foo http.decoded_foo; -urldecode.js: +http.js: function decoded_foo(r) { return decodeURIComponent(r.args.foo); } + +export default {decoded_foo}; @@ -75,9 +82,9 @@ function decoded_foo(r) { nginx.conf: -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 @@ location / { -urlencode.js: +http.js: function encoded_foo(r) { return encodeURIComponent('foo & bar?'); } + +export default {encoded_foo}; @@ -103,10 +112,10 @@ function encoded_foo(r) { nginx.conf: -js_include redirect.js; +js_import http.js; location /redirect { - js_content redirect; + js_content http.redirect; } location @named { @@ -116,11 +125,13 @@ location @named { -redirect.js: +http.js: function redirect(r) { r.internalRedirect('@named'); } + +export default {redirect}; @@ -132,10 +143,10 @@ function redirect(r) { nginx.conf: -js_include fastresponse.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /foo { @@ -149,7 +160,7 @@ location /bar { -fastresponse.js: +http.js: function content(r) { var n = 0; @@ -163,6 +174,8 @@ function content(r) { r.subrequest('/foo', r.variables.args, done); r.subrequest('/bar', r.variables.args, done); } + +export default {content}; @@ -174,14 +187,14 @@ function content(r) { nginx.conf: -js_include hs_jwt.js; +js_import http.js; -js_set $jwt jwt; +js_set $jwt http.jwt; -hs_jwt.js: +http.js: function generate_hs256_jwt(claims, key, valid) { var header = { typ: "JWT", alg: "HS256" }; @@ -208,6 +221,8 @@ function jwt(r) { return generate_hs256_jwt(claims, 'foo', 600); } + +export default {jwt}; @@ -219,17 +234,17 @@ function jwt(r) { nginx.conf: -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 @@ location /api { -subrequest.js: +http.js: function set_keyval(r) { r.subrequest('/api/5/http/keyvals/foo', @@ -266,6 +281,8 @@ function version(r) { r.return(200, json.version); }); } + +export default {set_keyval, version}; @@ -277,9 +294,9 @@ function version(r) { nginx.conf: -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 @@ location @login { -hash.js: +http.js: function create_secure_link(r) { return require('crypto').createHash('md5') .update(r.uri).update(" mykey") .digest('base64url'); } + +export default {create_secure_link}; @@ -314,9 +333,9 @@ function create_secure_link(r) { nginx.conf: -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 @@ server { -js_requests.js: +http.js: function num_requests(r) { @@ -345,6 +364,8 @@ function num_requests(r) r.variables.foo = n; return n; } + +export default {num_requests}; The and @@ -360,17 +381,12 @@ are available as part of our
-The example works since -0.3.8. - - - nginx.conf: -js_include subrequests_chaining.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /auth { @@ -384,7 +400,7 @@ location /backend { -subrequests_chaining.js: +http.js: function content(r) { r.subrequest('/auth') @@ -401,6 +417,8 @@ function content(r) { }) .catch(_ => r.return(500)); } + +export default {content}; diff --git a/xml/ru/docs/njs/examples.xml b/xml/ru/docs/njs/examples.xml --- a/xml/ru/docs/njs/examples.xml +++ b/xml/ru/docs/njs/examples.xml @@ -9,7 +9,16 @@
+ rev="13"> + +
+ + +Примеры работают начиная с версии +0.4.0. + + +
@@ -21,26 +30,21 @@ load_module modules/ngx_http_js_module.s events {} http { - js_include hello_world.js; - - server { - listen 8000; - - location / { - js_content hello; - } - } + js_import http.js; + js_content http.hello; } -hello_world.js: +http.js: function hello(r) { r.return(200, "Hello world!"); } + +export default {hello}; @@ -52,18 +56,20 @@ function hello(r) { nginx.conf: -js_include urldecode.js; +js_import http.js; -js_set $decoded_foo decoded_foo; +js_set $decoded_foo http.decoded_foo; -urldecode.js: +http.js: function decoded_foo(r) { return decodeURIComponent(r.args.foo); } + +export default {decoded_foo}; @@ -75,9 +81,9 @@ function decoded_foo(r) { nginx.conf: -js_include urlencode.js; +js_import http.js; -js_set $encoded_foo encoded_foo; +js_set $encoded_foo http.encoded_foo; ... location / { @@ -87,11 +93,13 @@ location / { -urlencode.js: +http.js: function encoded_foo(r) { return encodeURIComponent('foo & bar?'); } + +export default {encoded_foo}; @@ -103,10 +111,10 @@ function encoded_foo(r) { nginx.conf: -js_include redirect.js; +js_import http.js; location /redirect { - js_content redirect; + js_content http.redirect; } location @named { @@ -116,11 +124,13 @@ location @named { -redirect.js: +http.js: function redirect(r) { r.internalRedirect('@named'); } + +export default {redirect}; @@ -132,10 +142,10 @@ function redirect(r) { nginx.conf: -js_include fastresponse.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /foo { @@ -149,7 +159,7 @@ location /bar { -fastresponse.js: +http.js: function content(r) { var n = 0; @@ -163,6 +173,8 @@ function content(r) { r.subrequest('/foo', r.variables.args, done); r.subrequest('/bar', r.variables.args, done); } + +export default {content}; @@ -174,14 +186,14 @@ function content(r) { nginx.conf: -js_include hs_jwt.js; +js_import http.js; -js_set $jwt jwt; +js_set $jwt http.jwt; -hs_jwt.js: +http.js: function generate_hs256_jwt(claims, key, valid) { var header = { typ: "JWT", alg: "HS256" }; @@ -208,6 +220,8 @@ function jwt(r) { return generate_hs256_jwt(claims, 'foo', 600); } + +export default {jwt}; @@ -219,17 +233,17 @@ function jwt(r) { nginx.conf: -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 +253,7 @@ location /api { -subrequest.js: +http.js: function set_keyval(r) { r.subrequest('/api/5/http/keyvals/foo', @@ -266,6 +280,8 @@ function version(r) { r.return(200, json.version); }); } + +export default {set_keyval, version}; @@ -277,9 +293,9 @@ function version(r) { nginx.conf: -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 +312,15 @@ location @login { -hash.js: +http.js: function create_secure_link(r) { return require('crypto').createHash('md5') .update(r.uri).update(" mykey") .digest('base64url'); } + +export default {create_secure_link}; @@ -314,9 +332,9 @@ function create_secure_link(r) { nginx.conf: -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 +354,7 @@ server { -js_requests.js: +http.js: function num_requests(r) { @@ -345,6 +363,8 @@ function num_requests(r) r.variables.foo = n; return n; } + +export default {num_requests}; Директивы и @@ -360,17 +380,12 @@ function num_requests(r)
-Пример работает начиная с версии -0.3.8. - - - nginx.conf: -js_include subrequests_chaining.js; +js_import http.js; location /start { - js_content content; + js_content http.content; } location /auth { @@ -384,7 +399,7 @@ location /backend { -subrequests_chaining.js: +http.js: function content(r) { r.subrequest('/auth') @@ -401,6 +416,8 @@ function content(r) { }) .catch(_ => r.return(500)); } + +export default {content};