我想将所有不匹配的网址重定向到我的主页.IE浏览器.有人去www.mysite.com/blah/blah/blah/foo/bar或www.mysite.com/invalid_url- 我想重定向他们www.mysite.com
显然我不想干涉我的有效网址.
那么我是否可以使用一些通配符匹配器将请求重定向到这些无效的URL?
Dan*_*iel 35
在其余路线的末尾添加路线.
app.all('*', function(req, res) {
res.redirect("http://www.mysite.com/");
});
Run Code Online (Sandbox Code Playgroud)
rob*_*lep 20
您可以在Express链中插入一个'catch all'中间件作为最后的中间件/路由:
//configure the order of operations for request handlers:
app.configure(function(){
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.static(__dirname+'/assets')); // try to serve static files
app.use(app.router); // try to match req with a route
app.use(redirectUnmatched); // redirect if nothing else sent a response
});
function redirectUnmatched(req, res) {
res.redirect("http://www.mysite.com/");
}
...
// your routes
app.get('/', function(req, res) { ... });
...
// start listening
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
我使用这样的设置来生成自定义404 Not Found页面.
| 归档时间: |
|
| 查看次数: |
17857 次 |
| 最近记录: |