My goal is to modify the response body of a POST route in Elysia, but after analyzing CTX, it seems that only the request is available and I am unable to locate any information on how to alter the response. Specifically, I aim to assign a task ID to users once they have uploaded a video file for processing (which may take a considerable amount of time), so I initiate the processing in the background and provide the user with a task ID.
app.post("/upload", async (ctx: any) => {
const taskId = uuidv4(); // generate taskId first
console.log(ctx)
try {
const f = ctx.body.video;
if (!f) {
ctx.status = 400;
ctx.body = { error: 'No video file provided' };
return;
}
const fileName = `${taskId}.mp4`;
const fileNameOut = `${taskId}.webm`;
const filePath = `${uploadDir}/${fileName}`;
const outputPath = `${outputDir}/${fileNameOut}`;
await Bun.write(Bun.file(filePath), f);
// Start the asynchronous video processing
// processVideo(taskId, filePath, outputPath);
} catch (error) {
console.error('Error in /upload route:', error);
ctx.status = 500;
ctx.body = { error: 'Internal Server Error' };
}
// Always respond with the taskId even when an error occurs
if (ctx) {
ctx.status = 200;
ctx.body = { taskId: taskId }; // give the user their video task id
} else {
console.log('Context (ctx) is not defined correctly');
}
});
Manipulating ctx.body successfully changes the request body to display the correct taskID, resulting in a 200 status code in the response, although the body remains empty.
Unfortunately, ctx.response is undefined and my testing script returns an 'Error: Network response was not ok.' message.
I have thoroughly reviewed the documentation for Elysia and have exhausted all efforts trying to pinpoint the issue, without success.
Despite attempting various methods and tools such as express and multer, I encountered a bug with bun 1.2 and multer causing a hang. All I require is for the post request response to include a task ID confirmation if initiated successfully.