# HG changeset patch # User Yaroslav Zhuravlev # Date 1579624674 0 # Node ID b60e5be733cdc5078845a9c94c9c308c4f865bff # Parent 9bf87153b1b3b0308fe51d63b5cc2242acf581b0 Added Example of promisified requests to njs. 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,7 @@
+ rev="8">
@@ -353,4 +353,51 @@ are available as part of our
+ +
+ + +The example works since +0.3.8. +nginx.conf: + +js_include promisified_subrequest.js; + +location /start { + js_content content; +} + +location /auth { + proxy_pass http://auth_backend; +} + +location /backend { + proxy_pass http://backend; +} + + + + +promisified_subrequest.js: + +function content(r) { + r.subrequest(r, '/auth') + .then(reply => JSON.parse(reply.responseBody)) + .then(response => { + if (!response['token']) { + throw new Error("token is not available"); + } + return token; + }) + .then(token => { + r.subrequest('/backend', `token=${token}`) + .then(reply => r.return(reply.status, reply.responseBody)); + }) + .catch(_ => r.return(500)); +} + + + +
+
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,7 @@
+ rev="8">
@@ -353,4 +353,51 @@ function num_requests(r)
+ +
+ + +Пример работает начиная с версии +0.3.8. +nginx.conf: + +js_include promisified_subrequest.js; + +location /start { + js_content content; +} + +location /auth { + proxy_pass http://auth_backend; +} + +location /backend { + proxy_pass http://backend; +} + + + + +promisified_subrequest.js: + +function content(r) { + r.subrequest(r, '/auth') + .then(reply => JSON.parse(reply.responseBody)) + .then(response => { + if (!response['token']) { + throw new Error("token is not available"); + } + return token; + }) + .then(token => { + r.subrequest('/backend', `token=${token}`) + .then(reply => r.return(reply.status, reply.responseBody)); + }) + .catch(_ => r.return(500)); +} + + + +
+