Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Can I not redirect page to client side after send?

I'm trying to redirect page when i send a data from oAuth , however, I can not reach to client side by invoke res.redirect so i tried to put it on anywhere but it still doesn't work .

and i got error like : Cannot set headers after they are sent to the client

How can I change the url with redirect?? if it can't what is another alternative ??

Thank you in advance !

Here is the code :

app.get("/callback", cors(), (req, res) => {
 

  if (req.query.code !== null && req.query.code !== undefined) {
    var token_url = "https://www.oauthserver/oauth2.0/token"; //this is Oauth server
    var options = {
      url: token_url,
      method: "POST",
      form: {
        grant_type: "authorization_code",
        client_id: client_id,
        client_secret: client_secret,
        redirect_uri: redirectURI,
        code: req.query.code,
        state: req.query.state,
      },
    };

 

    request(options, function (error1, response1, body1) {
      if (!error1 && response1.statusCode == 200) {
        var tdata = JSON.parse(body1);
        var options2 = {
          url: "https://www.oauthserver/oauth2.0/resource", //this is Oauth server
          headers: {
            "Content-Type": "application/json",
            Authorization: "Bearer " + tdata.access_token,
          },
        };
        request(options2, function (error2, response2, body2) {
          if (!error2 && response2.statusCode == 200) {
            res.redirect("http://localhost:3000"); //client Side
            res.status(200).send(body2);
          
            console.log(JSON.parse(body2));
          
          } else {
            res.status(response2.statusCode).end();
            console.log("error2 = " + response2.statusCode);
          }
        });
      } else {
        res.status(response1.statusCode).end();
        console.log("error1 = " + response1.statusCode);
      }
    });
  }
});

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda