portfolio/server/routes/scores/[...path].ts

26 lines
660 B
TypeScript
Raw Normal View History

import { existsSync, createReadStream } from 'fs'
import { join } from 'path'
import { sendStream } from 'h3'
import { createError } from 'h3'
export default defineEventHandler(async (event) => {
const url = event.path
// Get the filename from the URL
const filename = url.replace('/scores/', '')
// Check if file exists in public/scores/
const filePath = join(process.cwd(), 'public/scores', filename)
if (!existsSync(filePath)) {
throw createError({
statusCode: 404,
statusMessage: 'Not Found'
})
}
// Serve the file
event.node.res.statusCode = 200
return sendStream(event, createReadStream(filePath))
})