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