Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Node.js uygulamalar için proxy'leri etkinleştirmenin standart bir yolu yoktur. Proxy kullanıp kullanamayacağınız, HTTP isteklerinde bulunmak için kullandığınız kitaplığa bağlıdır. Genellikle, proxy'yi yapılandırmak için kodunuzu güncelleştirmeniz gerekir. Ancak paketi kullanarak global-agent
en az kod değişikliğiyle Node.js uygulamanız için ara sunucu desteğini etkinleştirebilirsiniz.
Yerel Node.js getirme API'si
v17.5.0'da Node.js API için fetch
deneysel destek sağlar. Ne yazık ki bu API hala sınırlıdır ve ara sunucu yapılandırmayı desteklemez. Dev Proxy'yi Node.js kullanmak istiyorsanız, HTTP isteklerinde bulunmak için farklı bir kitaplık kullanmanız gerekir.
global-agent
global-agent
Node.js için genel bir HTTP/HTTPS aracısı sağlayan popüler bir kitaplıktır. Ortam değişkenlerini kullanarak ara sunucuyu belirtmenize olanak tanır. Kullanmanın global-agent
avantajı, Geliştirme Proxy'sini kullanmak için uygulamanızda HTTP istekleri gönderme yönteminizi değiştirmeniz gerekmeyecek olmasıdır.
Aşağıda, global-agent
'yı node-fetch
kullanan bir Node.js uygulamasında nasıl kullanabileceğinize dair bir örnek verilmiştir:
import fetch from 'node-fetch';
import { bootstrap } from 'global-agent';
bootstrap();
(async () => {
const result = await fetch('https://jsonplaceholder.typicode.com/posts');
const jsonResult = await result.json();
console.log(JSON.stringify(jsonResult, null, 2));
})();
İşte Node.js ve standart https
modülünü global-agent
ile şu şekilde kullanabilirsiniz:
const https = require('https');
const globalAgent = require('global-agent');
globalAgent.bootstrap();
https.get('https://jsonplaceholder.typicode.com/posts', (resp) => {
let data = '';
resp.on('data', (d) => {
data += d;
});
resp.on('end', () => {
console.log(JSON.parse(data));
});
resp.on('error', (err) => {
console.error(err);
});
});
Uygulamanızı başlatırken GLOBAL_AGENT_HTTP_PROXY
ortam değişkenini kullanarak ara sunucuyu belirtin ve sertifika hatalarını yoksayın.
NODE_TLS_REJECT_UNAUTHORIZED=0 GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8000 node global-node-fetch.mjs
node-fetch
node-fetch , Node.js için bir uygulama sağlayan popüler bir fetch
kitaplıktır.
node-fetch
ortam değişkenlerini kullanarak proxy belirtmeyi desteklemez. Bunun yerine, özel bir temsilci oluşturmanız ve bunu fetch
yöntemine geçirmeniz gerekir.
Dev Proxy ile node-fetch
kullanarak, https-proxy-agent
paketini kullanarak bir aracı tanımlama örneği aşağıda verilmiştir.
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');
(async () => {
// Create a custom agent pointing to Dev Proxy
const agent = new HttpsProxyAgent('http://127.0.0.1:8000');
// Pass the agent to the fetch method
const result = await fetch('https://jsonplaceholder.typicode.com/posts', { agent });
const jsonResult = await result.json();
console.log(JSON.stringify(jsonResult, null, 2));
})();
Axios
Axios , Node.js'da HTTP istekleri göndermeye yönelik bir diğer popüler kitaplıktır. Axios, ortam değişkenlerini kullanarak ara sunucuyu belirtmenize veya aracıyı doğrudan istek yapılandırmasında belirtmenize olanak tanır.
Ortam değişkenleriyle Axios ve Dev Proxy kullanma
Axios ile Dev Proxy kullandığınızda ve ortam değişkenlerini kullanarak ara sunucuyu belirttiğinizde kodunuzu değiştirmeniz gerekmez. Tek yapmanız gereken ortam değişkenini https_proxy
ayarlamaktır ve Axios bunu istekte bulunmak için kullanır.
import axios from 'axios';
(async () => {
const result = await axios.get('https://jsonplaceholder.typicode.com/posts');
const response = result.data;
console.log(JSON.stringify(response, null, 2));
})();
Ortam değişkenini https_proxy
genel olarak veya uygulamanızı başlatırken belirtin.
https_proxy=http://127.0.0.1:8000 node axios.mjs
Anladım
benzer şekilde node-fetch
, Got ortam değişkenlerini kullanarak proxy belirtmeyi desteklemez. Bunun yerine, özelleştirilmiş bir aracı oluşturmanız ve bunu isteğe geçirmeniz gerekir.
Dev Proxy ile Got'i nasıl kullanabileceğinize yönelik bir örnek aşağıda verilmişti:
import got from 'got';
import { HttpsProxyAgent } from 'https-proxy-agent';
(async () => {
// Create a custom agent pointing to Dev Proxy
const agent = new HttpsProxyAgent('http://127.0.0.1:8000');
const result = await got('https://jsonplaceholder.typicode.com/posts', {
// Pass the agent to the fetch method
agent: {
https: agent
},
// Disable certificate validation
https: {
rejectUnauthorized: false
}
}).json();
console.log(JSON.stringify(result, null, 2));
})();
SuperAgent
SuperAgent
ortam değişkenlerini kullanarak proxy belirtmeyi desteklemez. Dev Proxy'yi SuperAgent ile kullanmak için eklentiyi superagent-proxy
yüklemeniz ve yöntemini kullanarak proxy
ara sunucuyu yapılandırmanız gerekir.
const superagent = require('superagent');
require('superagent-proxy')(superagent);
(async () => {
const result = await superagent
.get('https://jsonplaceholder.typicode.com/posts')
.proxy('http://127.0.0.1:8000')
// Disable certificate validation
.disableTLSCerts();
console.log(JSON.stringify(result.body, null, 2));
})();
Bilinen sorunlar
Node.js ile Dev Proxy kullandığınızda aşağıdaki sorunlarla karşılaşabilirsiniz.
UNABLE_TO_VERIFY_LEAF_SIGNATURE
hatası
Node.js ile Dev Proxy kullandığınızda aşağıdakine benzer bir hata alırsınız:
/Users/user/my-app/node_modules/node-fetch/lib/index.js:1501
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
^
FetchError: request to https://jsonplaceholder.typicode.com/posts failed, reason: unable to verify the first certificate
at ClientRequest.<anonymous> (/Users/user/my-app/node_modules/node-fetch/lib/index.js:1501:11)
at ClientRequest.emit (node:events:518:28)
at TLSSocket.socketErrorListener (node:_http_client:495:9)
at TLSSocket.emit (node:events:518:28)
at emitErrorNT (node:internal/streams/destroy:169:8)
at emitErrorCloseNT (node:internal/streams/destroy:128:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
type: 'system',
errno: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE',
code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
}
Bu sorunu çözmek için ortam değişkenini NODE_TLS_REJECT_UNAUTHORIZED
olarak 0
ayarlamanız gerekir. Uygulamanızı başlatırken genel olarak veya satır içinde tanımlayabilirsiniz.
NODE_TLS_REJECT_UNAUTHORIZED=0 node index.js