Refactor API server

This commit is contained in:
Nikita Karamov 2023-03-16 18:56:32 +01:00
parent 8c0ab7cd7c
commit 90585fcb68
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD

View file

@ -21,21 +21,27 @@
import http from "http";
const requestListener = async (request, response) => {
const buffers = [];
for await (const chunk of request) {
buffers.push(chunk);
if (request.method !== "POST") {
response.writeHead(405).end();
return;
}
const data = Buffer.concat(buffers).toString();
const searchParameters = new URLSearchParams(data);
const text = searchParameters.get("text") || "";
const instanceURL =
searchParameters.get("instance") || "https://mastodon.social/";
let data = "";
request.on("data", (chunk) => {
data += chunk.toString();
});
const finalURL = new URL("share", instanceURL);
finalURL.search = new URLSearchParams({ text }).toString();
request.on("end", () => {
const requestBody = new URLSearchParams(data);
const postText = requestBody.get("text") || "";
const instanceURL =
requestBody.get("instance") || "https://mastodon.social/";
response.writeHead(303, { Location: finalURL.toString() }).end();
const finalURL = new URL("share", instanceURL);
finalURL.search = new URLSearchParams({ postText }).toString();
response.writeHead(303, { Location: finalURL.toString() }).end();
});
};
if (!import.meta.env || import.meta.env.PROD) {