Mercurial > hg > nginx-site
comparison xml/en/docs/njs/examples.xml @ 2483:b60e5be733cd
Added Example of promisified requests to njs.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Tue, 21 Jan 2020 16:37:54 +0000 |
parents | 253641e268a8 |
children | ba9bfd064a61 |
comparison
equal
deleted
inserted
replaced
2482:9bf87153b1b3 | 2483:b60e5be733cd |
---|---|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | 7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
8 | 8 |
9 <article name="Examples" | 9 <article name="Examples" |
10 link="/en/docs/njs/examples.html" | 10 link="/en/docs/njs/examples.html" |
11 lang="en" | 11 lang="en" |
12 rev="7"> | 12 rev="8"> |
13 | 13 |
14 <section id="helloword" name="Hello World"> | 14 <section id="helloword" name="Hello World"> |
15 | 15 |
16 <para> | 16 <para> |
17 <path>nginx.conf</path>: | 17 <path>nginx.conf</path>: |
351 </note> | 351 </note> |
352 </para> | 352 </para> |
353 | 353 |
354 </section> | 354 </section> |
355 | 355 |
356 | |
357 <section id="promisified_subrequest" name="Promisified Subrequest"> | |
358 | |
359 <para> | |
360 The example works since | |
361 <link doc="changes.xml" id="njs0.3.8">0.3.8</link>. | |
362 <path>nginx.conf</path>: | |
363 <example> | |
364 js_include promisified_subrequest.js; | |
365 | |
366 location /start { | |
367 js_content content; | |
368 } | |
369 | |
370 location /auth { | |
371 proxy_pass http://auth_backend; | |
372 } | |
373 | |
374 location /backend { | |
375 proxy_pass http://backend; | |
376 } | |
377 </example> | |
378 </para> | |
379 | |
380 <para> | |
381 <path>promisified_subrequest.js</path>: | |
382 <example> | |
383 function content(r) { | |
384 r.subrequest(r, '/auth') | |
385 .then(reply => JSON.parse(reply.responseBody)) | |
386 .then(response => { | |
387 if (!response['token']) { | |
388 throw new Error("token is not available"); | |
389 } | |
390 return token; | |
391 }) | |
392 .then(token => { | |
393 r.subrequest('/backend', `token=${token}`) | |
394 .then(reply => r.return(reply.status, reply.responseBody)); | |
395 }) | |
396 .catch(_ => r.return(500)); | |
397 } | |
398 </example> | |
399 </para> | |
400 | |
401 </section> | |
402 | |
356 </article> | 403 </article> |