2016-12-12 7 views
0

https経由でHAPROXYと通信するnodejsアプリケーションを作成しようとしています。アイデアは、nodejsがhttps経由でhaproxyにメッセージを送信し、haproxyがメッセージを前方にルーティングするというものです。Node.jsを設定するHAPROXYと連携するHTTPS

私はrequest.jsライブラリを使用しましたが、すべて正常に動作しましたが、今はライブラリなしでこのタスクを実行する必要があります。シナリオは次のとおりです。環境変数が1の場合、HTTPを使用する必要があります。他の場合は-HTTPSです。問題は、httpsとhaproxyを使用すると「ソケットハングアップエラー」が発生するが、request.jsではすべて正常に動作するということです。ここに私のコードです。

const protocol = process.env.NODE_ENV === 1 ? require('http') : require('https'); 

その後、私はこの場合NODE_TLS_REJECT_UNAUTHORIZED=0

reportData(json) { 
    const req = protocol.request(this.options, (res) => { 
     res.on('error', (err) => { 
      this.logger.error(`Failed to report ${err.message}`) 
     }) 
    }); 
    req.write(JSON.stringify(json)); 
    req.end(); 
    req.on('error', (err) => { 
     this.logger.error(`Failed to report ${err.message}`); 
    }); 
} 

を使用するSSHキーを検証するために、CAを使用したくないので、私はソケットハングアップエラーしばらく取得HTTPS

this.api = url.parse(app.get('API_HAPROXY')); 
this.options = { 
    port: this.api.port, 
    hostname: this.api.hostname, 
    path: '/api/report', 
    method: 'POST', 
    headers: { 
     "Content-Type": "application/json", 
    }, 
    rejectUnauthorized: false, 
    requestCert: true, 
    agent: false 
}; 

を設定しますHTTPSを使用して

ここに私のリクエスト構成です

request({ 
    uri: `${this.api}/api/report`, 
    method: 'POST', 
    json, 
}, (err, response) => { 
    if (err || response.statusCode !== 200) { 
     this.logger.error(`Failed to report : ${err ? err.message : response.statusCode}`); 
    } else { 
     this.logger.info(`Report was sent`); 
    } 
}); 
+0

プロキシのURLにポートが含まれていますか? –

+0

'process.env.NODE_ENV === 1'私はenv値が文字列だと思っていますので、これは常にfalseです。 –

+0

はい。を含む。それは要求に応じて動作します。問題は環境にありません。httpsを使用しているので、確認しました –

答えて

0

コンテンツ長ヘッダーをoptions.headersに追加することで問題は解決しました。

this.api = url.parse(app.get('API_HAPROXY')); this.options = { 
port: this.api.port, 
hostname: this.api.hostname, 
path: '/api/report', 
method: 'POST', 
headers: { 
"Content-Type": "application/json", 
"Content-Length: <calculated length of the object you want to send in bytes > 
}, 
rejectUnauthorized: false, 
requestCert: true, 
agent: false 
}; 
関連する問題