【发布时间】:2021-09-15 07:33:07
【问题描述】:
我仅在生产模式下调用 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) { 类型:'系统', 错误号:'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],
}, {
headers: headers,
})
}).catch(err =>{
console.log(err.message)
})
return{
statusCode: 302,
headers: {
Location: 'https://open.spotify.com/playlist/'+state.playlisturi
}
}
}
一切都在本地运行netlify dev,但不是在生产模式下,它是在带有 SSL / TLS 加密的 https 上
【问题讨论】:
标签: node.js amazon-web-services netlify aws-serverless faunadb