如何解决Netlify 和 Faunadb 客户端网络套接字在建立安全 TLS 连接之前断开连接
我仅在生产模式下调用 Netlify AWS 无服务器函数时出现以下错误
ERROR FetchError:请求 https://registry.npmjs.org/faunadb 失败,原因:客户端网络套接字在安全 TLS 连接建立之前断开 在客户端请求。 (/var/task/node_modules/node-fetch/lib/index.js:1461:11) 在 ClientRequest.emit (events.js:314:20) 在 TLSSocket.socketErrorListener (_http_client.js:427:9) 在 TLSSocket.emit (events.js:314:20) 在emitErrorNT (internal/streams/destroy.js:92:8) 在emitErrorAndCloseNT (internal/streams/destroy.js:60:3) 在 processticksAndRejections (internal/process/task_queues.js:84:21) { 类型:'系统', errno: 'ECONNRESET',代码:'ECONNRESET' }
代码如下:
const faunadb = require('faunadb');
q = faunadb.query;
var fauna_client = new faunadb.Client({ secret: '[..]' });
const redirect_uri = "/.netlify/functions/follow_spotify_callback";
const client_id = "[..]";
const client_secret = "[..]";
const basic = Buffer.from(client_id + ":" + client_secret).toString('base64');
const qs = require('querystring');
const axios = require('axios');
exports.handler = async function(event,context) {
const state = JSON.parse(event.querystringparameters.state)
axios.post(
'https://accounts.spotify.com/api/token',qs.stringify({
'grant_type': 'authorization_code','code': event.querystringparameters.code,'redirect_uri': process.env.URL + redirect_uri
}),{
headers: {
'Authorization': `Basic ${basic}`,'Content-Type': 'application/x-www-form-urlencoded'
}
}
).then(res => {
const access_token = res.data.access_token;
const headers = {
'Authorization': `Bearer ${res.data.access_token}`,'Content-Type': 'application/json'
}
axios.get(
"https://api.spotify.com/v1/me",{
headers: headers,}
).then(result => {
fauna_client.query(
q.Exists(
q.Match(
q.Index('users_by_id'),result.data.id
)
)
).then(ret => {
if (ret == false){
fauna_client.query(
q.Create(
q.Collection('users'),{ data: {
display_name: result.data.display_name,id: result.data.id,access_token: access_token,campaign: state.artisturi,playlist: state.playlisturi,referred_by: state.referrer
} }
)
)
}else{
console.log("user already exists")
}
})
.catch(err => console.log(err));
}
).catch(err =>{
console.log(err.message)
});
axios.put('https://api.spotify.com/v1/me/following?type=artist',{
'ids': [state.artisturi],},{
headers: headers
});
axios.put(`https://api.spotify.com/v1/playlists/${state.playlisturi}/followers`,{
'public': true,{
headers: headers,});
axios.put('https://api.spotify.com/v1/me/tracks',{
'ids': [state.trackuri],})
}).catch(err =>{
console.log(err.message)
})
return{
statusCode: 302,headers: {
Location: 'https://open.spotify.com/playlist/'+state.playlisturi
}
}
}
一切都在本地运行 netlify dev
,但不是在生产模式下,它在 https 上使用 SSL / TLS 加密
解决方法
您似乎遇到了此处描述的问题:https://docs.fauna.com/fauna/current/drivers/known_issues
如果您将实例化 Fauna 客户端的代码移到处理程序逻辑中,问题可能会消失。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。