【问题标题】:Proxy API call then redirect to Angular app using Nginx代理 API 调用然后使用 Nginx 重定向到 Angular 应用程序
【发布时间】:2015-04-30 08:58:55
【问题描述】:

假设你访问这个网址:

https://example.com/AbCDeFghijK

...当AbCDeFghijK 由 API 验证时,您等待几毫秒,然后您将被重定向到:

https://example.com/?ref=SomeString

如果您使用 Nginx 为 Angular 应用程序提供服务,您将如何实现这一目标?

这个想法是实现类似于producthunt 在您单击链接但使用相同域时所做的事情。如果不使用 MEAN 应用程序或 another view 在我的 Angular 应用程序中,我还没有弄清楚如何完成这项工作。


其他信息

当您向 https://api.example.com/t/AbCDeFghijK 发出 GET 请求时,您将收到重定向到 https://example.com/?ref=SomeString

而且,当您向 https://example.com 发出 GET 请求时,您将访问由 Nginx 提供的 Angular 应用程序。

server {

  server_name my-angular-app;

  root /path/to/my-angular-app;

  location / {
    try_files $uri $uri/ /index.html;
  }
}

【问题讨论】:

  • 真的不清楚您要寻找的确切行为是什么。登录后留在 Angular 应用程序中还是加载 Angular 应用程序?
  • @charlietfl,我想在收到https://example.com/AbCDeFghijK的回复后加载一个角度应用程序
  • 这是一个 ajax 请求吗?仍然不清楚流程是什么
  • @charlietfl,这是对 api.example.com/t/:id 的 GET 请求,结果将重定向到 example.com/?ref=SomeString

标签: angularjs node.js nginx


【解决方案1】:

解决此问题的最佳方法是首先存储 URL,然后在后台进行 AJAX 调用以验证 URL,然后根据响应重定向用户。

var proxy = "https://api.example.com/t/";
var payload = $location.path();
$http.post(proxy + payload).
  success(function(data, status) {
    if(data.ref) return $location.path('/?ref=' + payload);
  }).
  error(function(data, status) {
    console.log(data,status);
  }
});

在这里,您需要对从路径中提取的有效负载执行一些验证,以确保在调用 API 之前它位于您的架构中。

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 2020-09-05
    • 2023-03-29
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    相关资源
    最近更新 更多