Server read data from public
Hello,
In server/api I have data script, that reads file from public, and returns json
When I build and run it locally, everything is fine; but when I try to deploy it with netlify I get empty response from api/data.
Most likely it is due to current working directory (
process.cwd()
) being outside of dist folder, however I have no idea, how else I can load that data, as relative path to public
differs between dev and build.3 Replies
Hi @𝔪𝔟𝔩𝔢. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To address your issue with reading files from the
public
directory in a Nuxt 3 server API route, especially when deploying to Netlify, we need to make some adjustments to your code. The problem likely stems from the way file paths are resolved in different environments. Here's a solution that should work both in development and production:
1. First, move your data file from public/personal/main.toml
to the server/
directory. This is because the public
directory is meant for static assets served directly by the web server, not for server-side file operations.
2. Update your server API route to use the correct path resolution method. Here's the modified code:
This approach uses import.meta.url
to get the current module's URL and then resolves the path to the main.toml
file relative to the current file's location. This should work consistently across different environments, including Netlify.
The reason for moving the file out of the public
directory is explained in the Nuxt 3 documentation on assets:
Nuxt won't serve files in theBy moving the file to theassets/
directory at a static URL like/assets/my-file.png
. If you need a static URL, use thepublic/
directory.
server/
directory, we ensure that it's not publicly accessible but can be read by your server-side code.
If you absolutely need to keep the file in the public
directory for some reason, you could try using the useRuntimeConfig
composable to access the public
directory path, as mentioned in the Nuxt 3 documentation on runtime config. However, this approach is less straightforward and may require additional configuration.
Remember to update your build settings on Netlify to ensure that Nuxt is building correctly for server-side rendering (SSR) mode.
Nuxt 3 Assets Documentation
Nuxt 3 Runtime Config Documentation
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@216277353951789065> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
I am currently trying to use this plugin - https://github.com/originjs/origin.js/tree/main/packages/vite-plugin-content; but it is not f-ing working
I need help 😥
I solved that problem