Saturday, 20 May 2017

Nodejs ejs always asking for view when trying to redirect a url and receive its json data

I am trying to make a slack app and to complete Oauth2, I have to send the URI below and get a JSON response back in the body.

The problem is, every time I am trying to use the function request() in my app.get() function, ejs is always trying to go and get my views. Now I tried rendering my specific view for app.get() but then when I use request() again, ejs is again trying to get a view.

How can I redirect to another url from my app.get and receive the JSON. I can use req.redirect() but I don't know how to get the response back.

Please please help! Thanks

app.get('/', (req, res) =>{
    var options = {
       uri: 'https://slack.com/api/oauth.access code='+req.query.code+'&client_id='+client_id+'&client_secret='+client_secret,
       method: 'GET'
    }
    request(options, (error, response, body) => {
       var JSONresponse = JSON.parse(body)
       if (!JSONresponse.ok){
          console.log(JSONresponse)
          res.send("Error encountered: \n"+JSON.stringify(JSONresponse)).status(200).end()
       }else{
         console.log(JSONresponse)
        res.send("Success!")
       }
    })
})



via user7278236

No comments:

Post a Comment