每当我的节点服务器返回404错误时,我希望我的Angular应用程序拦截它并将用户定向到某个路径。
我正在尝试构建一个简单的拦截器来执行此操作,但它似乎没有返回任何内容。
有关为什么在404进入时此代码不会打印任何内容的任何指示(我已通过查看页眉确认了这一点):
代码:
.config(function($httpProvider) {
//Ignore this authInceptor is for saving my JWT's
$httpProvider.interceptors.push('authInterceptor');
//This is the one I'm having issue with
$httpProvider.interceptors.push(function myErrorInterceptor($window, $location) {
return {
requestError: function(res){
if (res.status === 404) {
console.log('You're In - Now do some more stuff')
}
return res;
}
}
})
})
更新: 除此之外,我已经在节点方面实现了 - 我有
//This section is to handle Angular HTML5Mode Urls
//Have to rewrite all your links to entry point of your application (e.g. index.html)
app.get('/*', function(req, res) {
res.sendFile(__dirname + '/site/index.html')
});
这里的另一个问题是 - app.get('/*'
不能区分根据Angular路线允许的最新情况 - 所以我如何让它意识到这一点? (不想要一个我重复代码的数组)
答案 0 :(得分:0)
我认为问题在于您正在挂钩requestError
而不是responseError
。请尝试以下方法:
.config(function($httpProvider) {
//Ignore this authInceptor is for saving my JWT's
$httpProvider.interceptors.push('authInterceptor');
//This is the one I'm having issue with
$httpProvider.interceptors.push(function myErrorInterceptor($window, $location) {
return {
responseError: function(res){
if (res.status === 404) {
console.log('You're In - Now do some more stuff')
}
return res;
}
}
})
});