How to read files on an API route correctly with Vercel deployment

114 views Asked by At

I have an API route where I need to read all files from a directory and also do some computation with it. The files should not be public and are currently located inside the project root under /characters.

server/api/read-characters

import fs from 'fs';
import path from "path";

export default defineEventHandler(async (event) => {
    const folder = path.join(process.cwd(), 'characters')
    const files = fs.readdirSync(folder)
    return files;
})

This works fine when developing locally, but when deployed to Vercel, the serverless function has no access to the files and throws an error.

I know about Nitro server assets, but I think I can only address specific files using useStorage , not a whole folder directly?

How do I do this correctly?

Sidenote: the files are images that I overlay to create a new image. I have a quite large set of possible images > 50mb, which need to be available (but not all are used every time). The computation should be as fast as possible, because the functionality is used very often.

0

There are 0 answers