简体   繁体   English

重定向到Node.js中的默认语言?

[英]Redirect to default language in Nodejs?

Currently working on a Node.js/Express website where I have two languages setup (EN and FR). 目前正在Node.js / Express网站上工作,在该网站上我有两种语言设置(EN和FR)。 If the user enters www.domain.com (=no language set) I would like it to be redirected to the startpage for the default language - in this case EN, so www.domain.com/en but if the user enters www.domain.com/fr it should load startpage for FR language. 如果用户输入www.domain.com(=未设置语言),我希望将其重定向到默认语言的起始页-在这种情况下为EN,因此,如果用户输入www,则为www.domain.com/en。 domain.com/fr,它应加载FR语言的起始页。

EDIT: How would I correctly do this in my backend, so that the redirect happens asap and correct? 编辑:我将如何在我的后端正确执行此操作,以便尽快进行重定向并正确进行?

Thank you 谢谢

You don't need to do a redirect, just use the same route handler function for both the root and language specific paths: 您无需进行重定向,只需对根目录和特定于语言的路径使用相同的路由处理程序功能:

router.get('/en', handleEnglish);
router.get('/', handleEnglish);
router.get('/fr', handleFrench)

If you want to go with a redirect to make the URLs explicit, you can do the following: 如果要进行重定向以使URL显式显示,则可以执行以下操作:

router.get('/en', handleEnglish);
router.get('/fr', handleFrench)
router.get('/', function(req, res) {
  res.redirect('/en');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM