import Cors from "cors"; import fs from "fs"; // Initializing the cors middleware // You can read more about the available options here: https://github.com/expressjs/cors#configuration-options const cors = Cors({ methods: ["POST"], }); // Helper method to wait for a middleware to execute before continuing // And to throw an error when an error happens in a middleware function runMiddleware(req, res, fn) { return new Promise((resolve, reject) => { fn(req, res, (result) => { if (result instanceof Error) { return reject(result); } return resolve(result); }); }); } export default async function handler(req, res) { // Run the middleware await runMiddleware(req, res, cors); const id = Math.floor(Math.random() * 90000) + 10000; const textData = req.body; let data = textData.split(","); let text = "Próbkowanie: 1\nSegment 0: w dół\nX: Y, Z\n"; let i = 1; for (let snippet of data) { if (i % 3 != 0) { text += snippet + ","; } else { text += snippet + "\n"; } i += 1; } // console.log(text); fs.writeFile(`externals/${id}.txt`, text, (err) => { if (err) { res.status(418); throw err; } }); // console.log(textData); res.status(200).send({ url: `https://test.wastpol.pl/?external=tru&id=${id}` }); }