Skip to content

Commit d8937c6

Browse files
committed
Update JsonPost.js example
1 parent ccd8050 commit d8937c6

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

‎examples/JsonPost.js‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const app = uWS./*SSL*/App({
1919

2020
res.end('Thanks for this json!');
2121
}, () => {
22-
/* Request was prematurely aborted, stop reading */
23-
console.log('Ugh!');
22+
/* Request was prematurely aborted or invalid or missing, stop reading */
23+
console.log('Invalid JSON or no data at all!');
2424
});
2525

2626
}).listen(port, (token) => {
@@ -38,10 +38,25 @@ function readJson(res, cb, err) {
3838
res.onData((ab, isLast) => {
3939
let chunk = Buffer.from(ab);
4040
if (isLast) {
41+
let json;
4142
if (buffer) {
42-
cb(JSON.parse(Buffer.concat([buffer, chunk])));
43+
try {
44+
json = JSON.parse(Buffer.concat([buffer, chunk]));
45+
} catch (e) {
46+
/* res.close calls onAborted */
47+
res.close();
48+
return;
49+
}
50+
cb(json);
4351
} else {
44-
cb(JSON.parse(chunk));
52+
try {
53+
json = JSON.parse(chunk);
54+
} catch (e) {
55+
/* res.close calls onAborted */
56+
res.close();
57+
return;
58+
}
59+
cb(json);
4560
}
4661
} else {
4762
if (buffer) {

0 commit comments

Comments
 (0)