【问题标题】:Prevent ANGULAR pages from rendering if NODE proxy is not running如果 NODE 代理未运行,则防止 ANGULAR 页面呈现
【发布时间】:2014-10-30 18:52:17
【问题描述】:

我在前端有一个节点代理和角度。 如果节点代理未运行,有没有办法可以始终重定向到(服务器关闭!对不起!)的角度页面?我不想让人们在没有节点代理的情况下看到其他更安全的页面。

我正在考虑使用 HTTP 状态,但是有什么简单的方法吗?

谢谢!

【问题讨论】:

    标签: javascript angularjs node.js proxy


    【解决方案1】:

    $http interceptors 可以在全球范围内为您做到这一点

    使用拦截器,您可以查看每个请求的响应。我建议使用 HTTP 状态,特别是 503 "Service Unavailable"(服务器当前不可用(因为它超载或停机维护)。一般来说,这是一个临时状态。)

    $httpProvider.interceptors.push(function($location) {
      var handleResponse = function(response) {
         if(response.status === 503){
            $location.path('/503.html'); // not sure if you are using ui-router or how your routing is set up...
         }else{
            return response;
         }
      };
      return {
        'response': handleResponse,
        'responseError': handleResponse,
      };
    });
    

    【讨论】:

    • 酷!从文档看来,它必须存在于服务中。这可以在 app.module 中的 .run() 函数中工作吗?
    • 因为我使用的是 stateProvider,我是否应该让拦截器包围我的所有状态,以便在每个状态下进行检查?例如) $httpProvider.interceptors.push(function($location) { var handleResponse = function(response) { stateprovider.state() }; return { 'response': handleResponse, 'responseError': handleResponse, }; });
    • 您可以在$location 旁边注入$state,然后您可以执行$state.go(...)stateProvider 用于设置状态,$state.go() 用于转换到状态。
    • 与发布的问题不同的问题。现在使用拦截器,它似乎发生在角度页面开始呈现之后。因此,在它重新路由到我想要它的状态之前,我得到了这个安全页面的闪光,有什么提示/想法可以使它无缝?
    • 嗨!我最终修复了它:) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多