2017-08-07 17 views
0

私のリクエストにカスタムhttpエージェントを設定する(とkeepAliveを設定する)のは、とりわけrequest.defaults(..)を使用しています。Node.js - リクエスト:http&httpsを処理できるカスタムエージェントを指定する

const agent = new http.Agent({ 
     keepAlive: true, 
     maxSockets: 42 
     }) 
const requestWithCustomAgent = request.defaults({ 
     agent, 
     json: true, 
     headers: { 
      'User-Agent': 'amazing agent' 
     }   
     }) 

Error: Protocol "https:" not supported. Expected "http:"でスキームhttpsと、このエラーがURLにアクセスします。逆の場合はhttps.Agentとスキームhttpです。

http要求をあるエージェントで処理し、httpsを別のエージェントで処理する要求を「伝える」方法はありますか?

一つは、いくつかのユニコーンの血を投資し、同様に黒魔術を行うことができます:

if(url.startsWith('https') {/* use on agent */} else { /* use the other */} 

しかし、それはhack'ish感じています。

const requestWithCustomAgent = request.defaults({ 
    agents: { 
     'http': httpAgent, 
     'https': httpsAgent 
    }, 
    json: true, 
    headers: { 
     'User-Agent': 'amazing agent' 
    }   
    }) 

可能:

のようなものですか?

ヘルプ感謝;)

答えて

1

あなたの代わりにhttp(s).Agentインスタンスを渡すのagentOptionsを使用する場合は、requestが使用されている各スキームのためにあなたのためのエージェントを作成します。

+0

がよりOPが何であるかのような音 – James

+0

ニースなので、(maxSocketsなどの)制限は「スキームごと」に適用されますか?例えばhttpエージェントは "cert"、 "key"などのオプションを無視します。 –

+0

@FlorianBarth correct。 – robertklep

関連する問題