summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexis Hovorka <[email protected]>2024-02-13 23:08:50 -0700
committerAlexis Hovorka <[email protected]>2024-02-13 23:08:50 -0700
commitc698ccab0cffe7212ef86da88a5f1da4ee81f5c8 (patch)
treeeb36aa336c77fc4b8d417171dad6109902ea4cdb
parent0278a785d3ae63117215050899ac4b053bfe3e55 (diff)
[feat] Add JSON post parsing convenience method
-rw-r--r--app/lib/router.js15
1 files changed, 14 insertions, 1 deletions
diff --git a/app/lib/router.js b/app/lib/router.js
index 8c0a2ef..fd24693 100644
--- a/app/lib/router.js
+++ b/app/lib/router.js
@@ -27,9 +27,22 @@ class Router {
};
}
- gpost(re, cb, max) { // Laziness
+ gpost(re, cb, max) {
this.post(re, this.gather(cb, max));
}
+
+ jpost(re, cb, max) {
+ this.gpost(re, (req, res, match, data) => {
+ try {
+ data = JSON.parse(data);
+ } catch (e) {
+ res.writeHead(400, {"Content-Type": "text/plain"});
+ return res.end("400 Bad Request");
+ }
+
+ cb(req, res, match, data);
+ }, max);
+ }
}
["get", "post", "put", "delete"].forEach(method =>