cómo redirigir en node.js

I'm having some issues with redirecting between directories, the problem is I can manage to redirect to another file in different directory. My directory structure looks like this:

-views
  -add_user.jade

-routes
  -index.js

I try to redirect to add_ usuario.jade de index.js, how would you guys do it

res.redirect('???');

If index.js en el mismo directorio que vistas, the code below works

-index.js
-views
  -add_user.jade

res.redirect('./add_users');

preguntado el 31 de julio de 12 a las 13:07

3 Respuestas

You want to redirect to the Enlance (not the view name) that you want the user to go to.

For example, if the route "/user/add" hace el "add_user.jade" view then you want to use

 res.redirect("/user/add");

Respondido 17 Jul 14, 18:07

This solution still works for the current Express. See este live Docker example. - user7503126

@user7503126's live Docker example isn't live anymore. - FranciscoBouza

Yes, you can redirect on Url with the help of

return res.redirect('/server/Thank'); 

Thank is a view file with Thank.jade extensión.

and route is /server/thank

Respondido el 14 de diciembre de 16 a las 10:12

Prueba esto:

document.location.href="full_URL";

Respondido 31 Jul 12, 13:07

The OP is asking about server side JavaScript, while your answer only works in the browser - yi jiang

And if your suggestion is "Return a 200 response with an HTML document containing some JavaScript that does the redirect" then that is probably the more unreliable, inefficient way to do it … and it also breaks the back button. - Quentin

@ChrisHawkes it seems like every person who upvoted the best answer has also cared for this one. - vrintle

The document object is a global that is only available for the browsers. In Node you need to use res.redirect. - matoneski

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.