2017-01-30 10 views
0

私のノードサーバが404エラーを返すときはいつでも、これを私のAngularアプリケーションとユーザが特定のルートに向けて傍受したいと思う。角度 - 404 HTTPインターセプタ

私はこれを行うための単純なインターセプタを構築しようとしていますが、何も返されないようです。

404が入ってくる時にこのコードは何も印刷されませんなぜ上の任意のポインタ(私はページヘッダを見て、これを確認しました):

enter image description here

とコード:

.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; 
     } 
    } 
    }) 
}) 

UPDATE: だから、それに加えて、私は私のノード側で実現してきました - 私は

//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('/*'は角度ルートごとに許可されているものを区別するためのベールではありません - それでどのように認識するのですか? (私はコードを複製する配列を何かしたくない)

+0

これは役に立つかもしれない[Fiddle](http://jsfiddle.net/LvhRC/103/)です。また、拒否された返品約束にも注意してください。 –

+0

サーバー/ノード側ですべてのリクエストを角度エントリポイント(HTML5モード用)に戻す404を処理した場合、どのシナリオで404がスローされますか? – Developer

答えて

0

私は、responseErrorの代わりにrequestErrorにフックしているというのが問題だと思います。次のように試してみてください:

.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; 
     } 
    } 
    }) 
}); 
+0

試してみた - いいえ – userMod2

+0

質問を更新しました – userMod2

関連する問題