N
Nuxt2y ago
Tony

Server /api returning 404, but still working

I have a server route in /server/api/download.post.ts. When fetching it, the route seems to return a 404 Cannot find any route matching /api/download. However, this makes no sense as the route works fine and the code inside the route actually works - just when it finishes (it's non-sync code I believe, tried promisify-ing it to no luck) the event.node.res.end won't work. Any help?
4 Replies
Tony
TonyOP2y ago
// server/api/download.post.ts
export default defineEventHandler(async (event) => {
const { id, filename } = await readBody(event);

const audio = ytdl(id, {
quality: 'highestaudio',
filter: (x) => x.container === 'mp4'
}).on('error', (err) => handleError(event, err));

// if I uncomment this, it'll work fine, so it seems to be to do with ffmpeg?
//return event.node.res.end(JSON.stringify({ url: `/download.mp3` }));

ffmpeg(audio)
.output(path.join(process.cwd(), `/public/download.mp3`))
.on('end', (stdout, stderr) => {
// this logs, proving the route works
console.log('end')
// this will never work as the route returns 404 before it gets to it
return event.node.res.end(JSON.stringify({ url: `/download.mp3` }));
})
.on('error', console.error)
.run();
});
// server/api/download.post.ts
export default defineEventHandler(async (event) => {
const { id, filename } = await readBody(event);

const audio = ytdl(id, {
quality: 'highestaudio',
filter: (x) => x.container === 'mp4'
}).on('error', (err) => handleError(event, err));

// if I uncomment this, it'll work fine, so it seems to be to do with ffmpeg?
//return event.node.res.end(JSON.stringify({ url: `/download.mp3` }));

ffmpeg(audio)
.output(path.join(process.cwd(), `/public/download.mp3`))
.on('end', (stdout, stderr) => {
// this logs, proving the route works
console.log('end')
// this will never work as the route returns 404 before it gets to it
return event.node.res.end(JSON.stringify({ url: `/download.mp3` }));
})
.on('error', console.error)
.run();
});
No description
warflash
warflash2y ago
Am I understanding correctly that you tried wrapping the ffmpg in a function that returns a promise and is then awaited? Or what did you do to make it be handled in an async context Was thinking something like this perhaps:
// Wrap the ffmpeg process in a Promise
const processAudio = () => new Promise((resolve, reject) => {
ffmpeg(audio)
.output(path.join(process.cwd(), `/public/download.mp3`))
.on('end', (stdout, stderr) => {
resolve({ url: `/download.mp3` });
})
.on('error', (error) => {
console.error(error);
reject(error);
})
.run();
});

try {
// Wait for the ffmpeg process to complete
const result = await processAudio();
// Return the response instead of using event.node.res.end()
return JSON.stringify(result);
} catch (error) {
// Handle error if the ffmpeg process fails
handleError(event, error);
}
// Wrap the ffmpeg process in a Promise
const processAudio = () => new Promise((resolve, reject) => {
ffmpeg(audio)
.output(path.join(process.cwd(), `/public/download.mp3`))
.on('end', (stdout, stderr) => {
resolve({ url: `/download.mp3` });
})
.on('error', (error) => {
console.error(error);
reject(error);
})
.run();
});

try {
// Wait for the ffmpeg process to complete
const result = await processAudio();
// Return the response instead of using event.node.res.end()
return JSON.stringify(result);
} catch (error) {
// Handle error if the ffmpeg process fails
handleError(event, error);
}
@Tony
Tony
TonyOP2y ago
Oh hell yeah, that worked great, thanks a bunch! I didn't realise that the ffmpeg library is pretty old and not promise/sync by default I'm only running into an issue with fs.unlink hanging now, otherwise working great. Thanks!
warflash
warflash2y ago
ah nice, you're welcome! 👌
Want results from more Discord servers?
Add your server