Proxy files through server
Hello. I use for backend Strapi. I upload files there. I can download them by the link http://localhost:1337/uploads/passkeys_b3a66b3a99.zip But localhost:1337 is an internal domain and I don't want make it external.
I need users can download file by nuxt domain (in dev it is http://localhost:3000). What is correct way to do it? When user fetch http://localhost:3000/uploads/passkeys_b3a66b3a99.zip Nuxt has to give the file from http://localhost:1337/uploads/passkeys_b3a66b3a99.zip
2 Replies
Hi @Yuri. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
You can proxy Strapi’s file URLs through your Nuxt app by adding a server route that forwards requests to Strapi. This way, users hit http://localhost:3000/uploads/... while Nuxt fetches from http://localhost:1337/uploads/....
In Nuxt 3, create a server route and stream the response:
- server/routes/uploads/[...path].ts
Notes:
- Place this in server/routes to expose it without the /api prefix, so /uploads/<file> is handled by the route. [server dir]
- If you run both services in Docker, the server-side request should target the Strapi container hostname (e.g., http://strapi:1337), not localhost, since “localhost” inside the Nuxt container refers to itself. [Retrieve API objects]
- Using a Nitro proxy route like this is one of the recommended strategies for proxying backend resources in Nuxt. [Proxy your API]
If you only need this in development, you could restrict the proxy to dev by checking process.dev and otherwise redirecting or denying.
Alternative: If you prefer to keep /uploads under /api, use server/api/uploads/[...path].ts instead, which would expose /api/uploads/... by default. [server dir]
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@740173469404364872> Kapa.ai is still learning and improving, please let me know how I did by reacting below.