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 api 来获取用户的最新 5 个存储库。无论我提供什么用户名,我都会收到此错误:
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
我每次都得到“未找到用户”输出。