Refactor API server
This commit is contained in:
parent
8c0ab7cd7c
commit
90585fcb68
1 changed files with 17 additions and 11 deletions
28
api/share.js
28
api/share.js
|
@ -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) {
|
||||||
|
|
Reference in a new issue