用Express处理重定向的方法

95 阅读1分钟

如何在服务器端重定向到其他页面

重定向在Web开发中很常见。你可以使用Response.redirect() 方法创建一个重定向。

res.redirect('/go-there')

这将创建一个302重定向。

301重定向是用这种方法制作的。

res.redirect(301, '/go-there')

你可以指定一个绝对路径(/go-there),一个绝对的url (https://anothersite.com),一个相对路径(go-there),或者使用.. ,回到一个级别。

res.redirect('../go-there')
res.redirect('..')

你也可以重定向到Referer HTTP头的值(如果没有设置,默认为/