Express GitHub API没有响应输出
我正在使用GithubAPI获取用户的最新5份回购协议。 无论我使用什么用户名,都会出现以下错误:Express GitHub API没有响应输出,express,github-api,Express,Github Api,我正在使用GithubAPI获取用户的最新5份回购协议。 无论我使用什么用户名,都会出现以下错误: router.get('/github/:username', (req, res) => { try { const options = { uri: `https://api.github.com/users/${req.params.username}/repos/per_page=5&sort=created:asc&a
router.get('/github/:username', (req, res) => {
try {
const options = {
uri: `https://api.github.com/users/${req.params.username}/repos/per_page=5&sort=created:asc&
client_id=${config.get('githubClientId')}&client_secret=${config.get('githubClientSecret')}`,
method: "GET",
headers: { 'user-agent': 'node.js' }
};
request(options, (error, response, body) => {
if (error) console.error(error);
if (response.statusCode !== 200)
res.status(404).json({ msg: "No user found" })
res.json(JSON.parse(body));
})
} catch (error) {
console.log(error.message);
res.status(500).send('Internal Server Error');
}
});
每次我都会收到“找不到用户”的输出。GitHub不再允许我们创建OAuth应用程序,而是必须生成个人访问令牌。我还使用了axios,因为请求已被弃用
http_outgoing.js:536
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
. 用别的东西。哇!没想到会这样。你有什么建议吗?axios和node fetch很流行。
const uri = encodeURI(
`https://api.github.com/users/${req.params.username}/repos?per_page=5&sort=created:asc`
);
const headers = {
'user-agent': 'node.js',
Authorization: `token ${config.get('githubToken')}`
};
const gitHubResponse = await axios.get(uri, { headers });
return res.json(gitHubResponse.data);
} catch (err) {
console.error(err.message);
return res.status(404).json({ msg: 'No Github profile found' });
}