@@ -897,7 +897,7 @@ Checking:
897
897
898
898
Subrequests chaining [http/subrequests_chaining]
899
899
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
900
- Subrequests chaining using JS promises .
900
+ Subrequests chaining.
901
901
902
902
nginx.conf:
903
903
@@ -936,32 +936,35 @@ example.js:
936
936
937
937
.. code-block :: js
938
938
939
- function process (r ) {
940
- r .subrequest (' /auth' )
941
- .then (reply => JSON .parse (reply .responseBody ))
942
- .then (response => {
943
- if (! response[' token' ]) {
944
- throw new Error (" token is not available" );
945
- }
946
- return response[' token' ];
947
- })
948
- .then (token => {
949
- r .subrequest (' /backend' , ` token=${ token} ` )
950
- .then (reply => r .return (reply .status , reply .responseBody ));
951
- })
952
- .catch (e => r .return (500 , e));
939
+ async function process (r ) {
940
+ try {
941
+ let reply = await r .subrequest (' /auth' )
942
+ let response = JSON .parse ((reply .responseBody ));
943
+ let token = response[' token' ];
944
+
945
+ if (! token) {
946
+ throw new Error (" token is not available" );
947
+ }
948
+
949
+ let backend_reply = await r .subrequest (' /backend' , ` token=${ token} ` );
950
+ r .return (backend_reply .status , backend_reply .responseBody );
951
+
952
+ } catch (e) {
953
+ r .return (500 , e);
954
+ }
953
955
}
954
956
955
957
function authenticate (r ) {
956
- if (r .headersIn .Authorization .slice (7 ) === ' secret' ) {
958
+ let auth = r .headersIn .Authorization ;
959
+ if (auth && auth .slice (7 ) === ' secret' ) {
957
960
r .return (200 , JSON .stringify ({status: " OK" , token: 42 }));
958
961
return ;
959
962
}
960
963
961
964
r .return (403 , JSON .stringify ({status: " INVALID" }));
962
965
}
963
966
964
- export default {process , authenticate}
967
+ export default {process , authenticate};
965
968
966
969
Checking:
967
970
@@ -971,17 +974,10 @@ Checking:
971
974
Token is 42
972
975
973
976
curl http://localhost/start
974
- SyntaxError: Unexpected token at position 0
975
- at JSON.parse (native)
976
- at anonymous (example.js:3)
977
- at native (native)
978
- at main (native)
977
+ Error: token is not available
979
978
980
979
curl http://localhost/start -H ' Authorization: Bearer secre'
981
980
Error: token is not available
982
- at anonymous (example.js:4)
983
- at native (native)
984
- at main (native)
985
981
986
982
Modifying response
987
983
------------------
0 commit comments