所以我正在一个网站上工作,该网站需要具有以下路径的链接:
localhost:3000/visit/:name
Run Code Online (Sandbox Code Playgroud)
重定向到外部网址,例如 google.com?affiliateID=2。重定向需要是 nofollow,状态码为 307。
该应用程序是通用的;使用 express 和客户端 react 的服务器端渲染,两者都通过 react-router 使用共享路由。
路由.js
<Route status={307} path="visit/:slug" />
Run Code Online (Sandbox Code Playgroud)
服务器.js
app.use((req, res) => {
match({routes, location: req.originalUrl}, (err, redirectLocation, renderProps) => {
if (err) {
res.status(500).send(err.message)
} else if (redirectLocation) {
res.redirect(302, redirectLocation.pathname + redirectLocation.search)
} else if (renderProps) {
const html = renderToString(<RouterContext {...renderProps} />)
const isAffiliateLink = renderProps.routes.filter((route) => {
return route.status === 307
}).length > 0
if (isAffiliateLink) {
return res.redirect(307, "http://google.com")
}
const markup = renderToStaticMarkup(<Document html={html} …Run Code Online (Sandbox Code Playgroud)