假设我有一个有路线/联系人的SPA
对于Express的直接请求(即不是index.html),我需要执行重定向。
在Express中,我可以像这样定义这条路线:
app.use(function(req,res,next) {
if (req.path === "/contact" ) {
return res.redirect("/?path=" + req.path);
}
next();
});
这意味着,如果用户提出直接请求,他们最终会在其地址栏中的“domain.com/?path=contact”上找到index.html。然后前端可以处理路由等。
但也许有办法这样做,所以用户看不到这个丑陋的网址?
编辑:在发送给用户的HTTP标头中包含此路径信息是否可以接受?
答案 0 :(得分:1)
我找到了一个将index.html文件作为响应发送的解决方案。这样地址栏就不会改变。
app.use(function(req,res,next) {
if (req.path === "/contact" ) {
res.sendFile(...);
return;
}
next();
});