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

239
Visualizações
Redirect with 301 status code in react js single page application

I've developed a web application in react js and I'm using the client-side routing. I have a few deleted product URLs and I want to redirect users to the product listings URL with HTTP Status code 301 when the user opens any of the deleted product URLs. For Example, I've one product URL www.abc.com/prod-detail/123 and this product is deleted so when the user opens this URL, I want him to redirect to www.abc.com/products with HTTPS status code 301.

I tried to achieve this using the below solution.

Code to redirect the user to the products listings from the product details.

productDetails.js

if(productNotFound){
window.location.href ="/products?redirect=301";
}

I created one proxy server using http-proxy-middleware in the application to filter the above requests (If products contain the redirect=301 param) and pass them to the Node server.

setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function (app) {    
    const filter = function (pathname, req) { 
        if(req.query && req.query.redirect==='301'){
            return true;
        }
        else {
            return false;
        }
    };
   
    const apiProxy = createProxyMiddleware(myfilter, {
        target: "http://localhost:8000/",//Node server
        changeOrigin: false,
        secure: false    
    });    
    app.use('/products',apiProxy);
};

In the Node JS server, I'm filtering the requests having query param as redirect=301 and If I found this type of request I just redirect with the status code 301.

inedx.js (Node)

router.get('/products', function (req, res, next) {
    if(req.query && req.query.redirect==='301'){    
    req.query = null;
    const hostUrl = 'http://' + req.get('host') + req.originalUrl.split('?')[0];
    //res.statusCode=301;
    res.redirect(301, hostUrl);
    }
    else{
        res.redirect(301, 'http://' + req.get('host') + req.originalUrl);
    }
});

The above solution is not working for me. I'm not receiving the request on the Proxy server. I'm not sure this is the correct way or not as I'm new to react js.

Thanks in advance for any assistance or insight.

Please also let me know if there is any other way to achieve this without using server-side routing.

I hope the question makes sense. Any help would be appreciated.

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