Closed victorbucutea closed 7 years ago
Think the custom pathRewrite
is what you are looking for.
Provide a custom function and you can implement your own rewrite logic; Including rewriting query params.
@victorbucutea You could also do:
onProxyReq: (proxyReq, req, res) => {
proxyReq.path += '?key=value';
},
In my case I needed to remove a query parameter from the path. Just for reference, I'll leave my implementation for this.
const querystring = require('querystring');
// ...
pathRewrite: (path, req) => {
let newPath = path.replace(/^\/api/, '/base/api');
// Strip query parameter _csrf
if (req.method === 'GET' && /_csrf=/.test(newPath)) {
const newQuery = { ...req.query }; // copy object
delete newQuery._csrf;
if (Object.keys(newQuery).length) {
// There were more query parameters than just _csrf
newPath = `${newPath.split('?')[0]}?${querystring.stringify(newQuery)}`;
} else {
// _csrf was the only query parameter
newPath = `${newPath.split('?')[0]}`;
}
}
return newPath;
},
Here's a more flexible approach using URL
to parse and modify the query:
onProxyReq: (proxyReq, req, res) => {
const baseUrl = `${proxyReq.protocol}//${proxyReq.host}`;
const url = new URL(proxyReq.path, baseUrl);
url.searchParams.append('key', process.env.API_KEY);
proxyReq.path = url.pathname + url.search;
}
I seem to have a simple problem, but with a long time to solve ...
What am I doing wrong as I can't find neither a document nor an example about this query parameter.