From c698ccab0cffe7212ef86da88a5f1da4ee81f5c8 Mon Sep 17 00:00:00 2001 From: Alexis Hovorka Date: Tue, 13 Feb 2024 23:08:50 -0700 Subject: [feat] Add JSON post parsing convenience method --- app/lib/router.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'app/lib') 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 => -- cgit v1.2.3-70-g09d2