Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Express.js route not working every time if redirecting

I have a really simple express.js server,

app.get("/:var", (req, res) => {
    console.log(req.params.var);
    res.redirect(301, "/");
});

Now on my browser, if I go to the following urls one after another

localhost:127/abc
localhost:127/abc
localhost:127/xyz
localhost:127/abc
localhost:127/xyz
localhost:127/xyz
localhost:127/abc

I get the following output in my server's terminal

abc
xyz

For some reason, if I remove the res.redirect(301, "/"), everything works as expected and I get the following output

abc
abc
xyz
abc
xyz
xyz
abc

Even if I restart the server, I cannot use a value that I had use before the restart. Only if I delete the cache from my browser, can I use that value again just once.

Can someone explain what is going on? I need to keep the redirect and also process the data from the url before redirecting and hopefully not have the client clearing their browser cache every time they want to use the site.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here it said: A 301 response is cacheable by default; i.e., unless otherwise indicated by the method definition or explicit cache controls - reference: https://httpwg.org/specs/rfc7231.html#status.301

So your seeing behavior is correct, you have to clear cache in browsers to see the new response.

The statement above also suggests the solution, you can set the cache header to tell browsers not to cache the response. Something like:

res.set("cache-control","no-store")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda