I am using vercel.json to proxy (rewrites) my project, but I don’t want to expose my backend api address in the repository. so, I am warderning is there some way to use process.env in vercel.json, Or is there any other alternative?
vercel.json unfortunately doesn’t support Environment variable. You can use Edge middleware to achieve this. Read more about it in our documentation here or more generally on the Edge Middleware page,
Another potential alternative if you’re using Next.js would be to define your rewrites in next.config.js
which supports environment variable references.
Thank you very much for your help. Let me try serverless fucntion
I am facing some problems and I hope to get some help,I tried to follow the document
try create a file middleware.ts
in root dir
import { rewrite } from '@vercel/edge';
export const config = {
matcher: ['/api/:path*'],
};
export default async function middleware(req: Request) {
const url = new URL(req.url);
const path = url.pathname.replace(/^\/api/, '');
url.pathname = path;
url.host = process.env.API_HOST || "localhost";
return rewrite(url);
}
What I want to achieve is to access all paths under the /api endpoint, proxying to the API host specified in the environment variable API_HOST (like: api.example.com:8080).
But it appears to be 502 when I try to access it
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.