From e23e59025189ec61fdab49d9eec1f086c99941f8 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Fri, 27 Aug 2021 15:49:51 +0200 Subject: [PATCH] Try implementing backend with Node's `http` --- api/toot.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/api/toot.js b/api/toot.js index 4b822e0..6559b48 100644 --- a/api/toot.js +++ b/api/toot.js @@ -16,20 +16,23 @@ along with this program. If not, see . */ -module.exports = function (req, res) { - let text = ""; - let instanceURL = "https://mastodon.social/"; - if (req.body) { - if (req.body.text) { - text = req.body.text; - } - if (req.body.instance) { - instanceURL = req.body.instance; - } - } +const http = require("http"); - return res.redirect( - 303, - instanceURL + "share?text=" + encodeURIComponent(text) - ); -}; +http + .createServer(async (req, res) => { + const buffers = []; + for await (const chunk of req) { + buffers.push(chunk); + } + const data = Buffer.concat(buffers).toString(); + const params = new URLSearchParams(data); + + const text = params.get("text") || ""; + const instanceURL = params.get("instance") || "https://mastodon.social/"; + + const finalURL = new URL("share", instanceURL); + finalURL.search = new URLSearchParams({ text }).toString(); + + res.writeHead(303, { Location: finalURL.toString() }).end(); + }) + .listen(8000);