在Node.js 12中,如果你使用axios库在进行HTTPS请求时遇到了SSL错误 "SSL routines: ssl_choose_client_version: unsupported protocol",可能是因为axios默认使用的TLS版本不受支持。
为了解决这个问题,你可以通过设置axios的httpsAgent来指定一个支持的TLS版本。
以下是一个示例代码,展示了如何在使用axios时解决这个问题:
const axios = require('axios');
const https = require('https');
const agent = new https.Agent({
rejectUnauthorized: false,
// 指定支持的TLS版本
secureProtocol: 'TLSv1_2_method'
});
axios.get('https://example.com', { httpsAgent: agent })
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
在上面的示例中,我们创建了一个https.Agent对象,并指定了支持的TLS版本为TLSv1.2。然后,我们将该httpsAgent对象作为axios的配置选项httpsAgent的值传递进去。
请注意,rejectUnauthorized选项被设置为false,这是为了忽略证书验证错误,只有在开发和测试环境中使用。在生产环境中,你应该设置为true,并提供正确的证书路径。
通过以上方法,你应该能够解决“axios在Node 12中的SSL错误:SSL routines:ssl_choose_client_version:unsupported protocol”问题。