Search code examples
node.jsnode-modulesgithub-apinode-request

Api for fetching github repos not working in Node.js


I have registered my app in Oauth in github

//@router GET api/profile/github/:githubUsername 
//@desc   get github profile 
//@access public
router.get('/github/:githubUsername', (req,res)=>{
try {
    const options = {
        uri:`https://api.github.com/users/${req.params.githubUsername}
            /repos?per_page=5&sort=created:asc&client_id=${config.get('githubClientId')}
            &client_secret=${config.get('githubSecretKey')}`,
        method: 'GET',
        headers: { 'user-agent':'node.js' }
    };

    request(options, (err, response, body)=>{

        if(err) console.error(err)

        if(response.statusCode!==200) return res.status(404).json({msg : 'Github user not found'})

        res.status(200).json(JSON.parse(body));

    })

} catch (error) {
    console.log(error);
    res.status(500).send('Server Error');
}
})

This URL is working when used directly in browser. It is giving status 404. I have checked with clientId and secret key they are working fine. I am also not sure about what URL to be pasted in Callback URL in github OAuth apps.

Thanks in advance.


Solution

  • The url line break was the problem. Thanks!