如何仅在开发环境中使用 proxyAgent
How use proxyAgent only in development env
我目前被迫在我的请求对象中注释或取消注释一行,这取决于我的应用程序当前 运行 我的本地系统还是生产服务器。
我已经尝试用 bool 变量解决这个问题,但它不起作用。这是我的代码
const dev = true;
const devProxy = dev
? {agent: new HttpsProxyAgent("http://proxy:80")}
: {};
myFunc: async access_token => {
const response = await fetch(
URL,
{
// agent: new HttpsProxyAgent("http://proxy:89´0"),
devProxy,
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${access_token.access_token}`
}
}
);
if (response.ok) {
return response.json();
}
throw new Error(
"bla" +
response.status +
" StatusText: " +
response.statusText
);
},
错误提示未使用代理。
我怎样才能正确地做到这一点?
你可以做很多事情,可以将对象隔离到另一个变量并设置代理。或者您也可以 Object.assign
agent
键。
最简单的是分配一个变量:
const dev = true;
const options = {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${access_token.access_token}`
}
}
if (dev) options.agent = new HttpsProxyAgent("http://proxy:80");
myFunc: async access_token => {
const response = await fetch(
URL,
options
);
if (response.ok) {
return response.json();
}
throw new Error(
"bla" +
response.status +
" StatusText: " +
response.statusText
);
},
我目前被迫在我的请求对象中注释或取消注释一行,这取决于我的应用程序当前 运行 我的本地系统还是生产服务器。
我已经尝试用 bool 变量解决这个问题,但它不起作用。这是我的代码
const dev = true;
const devProxy = dev
? {agent: new HttpsProxyAgent("http://proxy:80")}
: {};
myFunc: async access_token => {
const response = await fetch(
URL,
{
// agent: new HttpsProxyAgent("http://proxy:89´0"),
devProxy,
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${access_token.access_token}`
}
}
);
if (response.ok) {
return response.json();
}
throw new Error(
"bla" +
response.status +
" StatusText: " +
response.statusText
);
},
错误提示未使用代理。
我怎样才能正确地做到这一点?
你可以做很多事情,可以将对象隔离到另一个变量并设置代理。或者您也可以 Object.assign
agent
键。
最简单的是分配一个变量:
const dev = true;
const options = {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${access_token.access_token}`
}
}
if (dev) options.agent = new HttpsProxyAgent("http://proxy:80");
myFunc: async access_token => {
const response = await fetch(
URL,
options
);
if (response.ok) {
return response.json();
}
throw new Error(
"bla" +
response.status +
" StatusText: " +
response.statusText
);
},