Skip to content

Commit 712a493

Browse files
committed
[update] http/subrequests_chaining example using async/await
1 parent 4935efc commit 712a493

File tree

2 files changed

+40
-41
lines changed

2 files changed

+40
-41
lines changed

‎README.rst

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ Checking:
897897
898898
Subrequests chaining [http/subrequests_chaining]
899899
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
900-
Subrequests chaining using JS promises.
900+
Subrequests chaining.
901901

902902
nginx.conf:
903903

@@ -936,32 +936,35 @@ example.js:
936936

937937
.. code-block:: js
938938
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+
}
953955
}
954956
955957
function authenticate(r) {
956-
if (r.headersIn.Authorization.slice(7) === 'secret') {
958+
let auth = r.headersIn.Authorization;
959+
if (auth && auth.slice(7) === 'secret') {
957960
r.return(200, JSON.stringify({status: "OK", token:42}));
958961
return;
959962
}
960963
961964
r.return(403, JSON.stringify({status: "INVALID"}));
962965
}
963966
964-
export default {process, authenticate}
967+
export default {process, authenticate};
965968
966969
Checking:
967970

@@ -971,17 +974,10 @@ Checking:
971974
Token is 42
972975
973976
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
979978
980979
curl http://localhost/start -H 'Authorization: Bearer secre'
981980
Error: token is not available
982-
at anonymous (example.js:4)
983-
at native (native)
984-
at main (native)
985981
986982
Modifying response
987983
------------------

‎njs/http/subrequests_chaining.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
function process(r) {
2-
r.subrequest('/auth')
3-
.then(reply => JSON.parse(reply.responseBody))
4-
.then(response => {
5-
if (!response['token']) {
6-
throw new Error("token is not available");
7-
}
8-
return response['token'];
9-
})
10-
.then(token => {
11-
r.subrequest('/backend', `token=${token}`)
12-
.then(reply => r.return(reply.status, reply.responseBody));
13-
})
14-
.catch(e => r.return(500, e));
1+
async function process(r) {
2+
try {
3+
let reply = await r.subrequest('/auth')
4+
let response = JSON.parse((reply.responseBody));
5+
let token = response['token'];
6+
7+
if (!token) {
8+
throw new Error("token is not available");
9+
}
10+
11+
let backend_reply = await r.subrequest('/backend', `token=${token}`);
12+
r.return(backend_reply.status, backend_reply.responseBody);
13+
14+
} catch (e) {
15+
r.return(500, e);
16+
}
1517
}
1618

1719
function authenticate(r) {
18-
if (r.headersIn.Authorization.slice(7) === 'secret') {
20+
let auth = r.headersIn.Authorization;
21+
if (auth && auth.slice(7) === 'secret') {
1922
r.return(200, JSON.stringify({status: "OK", token:42}));
2023
return;
2124
}
2225

2326
r.return(403, JSON.stringify({status: "INVALID"}));
2427
}
2528

26-
export default {process, authenticate}
29+
export default {process, authenticate};

0 commit comments

Comments
 (0)