0

having this request :

 // set a cookie to requested locale
  app.get('setlocale/:locale', function (req, res) {
    res.cookie('locale', req.params.locale); //set language in cookie
     if(req.param.locale === "es"){
     res.redirect('/es'); //i'm redirecting depending on the url
     // this could also redirect to 
     // res.redirect('/es/terms');
     ...
     }
  });

this request is triggered by a selector list like

   <ul class="dropdown-menu" role="menu">
        <li class="uppercase"><a rel="" href="/setlocale/es/">ES</a></li>
        <li class="uppercase"><a rel="" href="/setlocale/de/">DE</a></li>
        <li class="uppercase"><a rel="" href="/setlocale/en/">EN</a></li>
   </ul>

What is the right header status code following good practices should return in this request? . considering is redirecting to "http://example.com/es" or "http://example.com/de" or "http://example.com/de/terms" after changing a cookie language. and if I have to set a status , where and how it would be set in this request.

thanks.

melpomene
  • 84,125
  • 8
  • 85
  • 148
MikZuit
  • 684
  • 5
  • 17

1 Answers1

1

Because you're explicitly redirecting the user to a different url from the one they navigated to in your anchor href, the correct code should be 302, which is the default in express.

Because you're writing a cookie, you probably don't want to change this to something like 308, which hints that the redirect is permanent - encouraging browsers to cache the response and potentially giving you problems if the user wants to change their locale.

Kraig Walker
  • 812
  • 13
  • 25