#Read Contents of files in the public directory
3 messages · Page 1 of 1 (latest)
Hi @spice shale. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the knowledge sources provided, I can see information about the public/ directory structure and Nitro's server capabilities, but there isn't specific documentation about reading files from the public directory in Nitro or h3.
However, I can provide some context based on what's available:
The public/ directory is used to serve static assets at the server root as-is. Files in this directory are served directly and are not modified by the build process.
For reading files in a Nitro/h3 context, you would typically use Node.js file system APIs since Nitro runs on Node.js (or compatible runtimes). While the knowledge sources don't provide a specific example for reading public directory files, they do show that Nitro supports server storage and can work with file streams.
For example, the documentation shows sending file streams in Nitro:
import fs from 'node:fs'
import { sendStream } from 'h3'
export default defineEventHandler((event) => {
return sendStream(event, fs.createReadStream('/path/to/file'))
})
To read files from the public directory specifically, you would need to resolve the path to your public directory and use Node.js fs module to read the file contents. However, the knowledge sources don't contain a complete example of this specific use case.