javascript - 页面刷新时的 Angular 'Cannot GET' 路由

标签 javascript angularjs node.js

我的 Angular 应用程序中有一个搜索功能,当执行时点击我的 API 以获取结果,然后使用 $location.url 重定向用户。一切都很好......直到我尝试重新加载结果页面。当我这样做时,我收到一条 Cannot GET/search/[search-term] 消息。

应用程序配置文件:

'use strict';

angular.module('gameApp')
  .config(function($routeProvider, $locationProvider) {
    $routeProvider

    .when('/player/:playerId', {
      templateUrl: 'player.html'
    })
    .when('/search/:searchTerm', {
      templateUrl: '/app/gmSearchResults/gmSearchResults.html'
    });

    $locationProvider.html5Mode({
      enabled: true,
      requireBase: false,
      rewriteLinks: false
    });
  });

相关的navbar.controller.js:

vm.getSearchResults = function(searchTerm) {
  searchService.resource.getResults({
    query: searchTerm
  }).$promise.then(function(results) {
    if (results.playerId) {
      $location.url('/customer/' + results.playerId);
    } else {
      saveLatest(results);
      $location.url('/search/' + searchTerm);
    }
  });
};

最佳答案

设置您的 Express 应用:

app.route('/*').get(function(req, res) { 
    return res.sendFile(path.join(config.root, 'index.html')); 
});

关于javascript - 页面刷新时的 Angular 'Cannot GET' 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521443/

相关文章:

javascript - Node.js 异步函数

javascript - 在 JavaScript 上设置 url

javascript - 如何在 Flot 中填充折线图而不将 y 轴强制为零?

angularjs - 广播模式的改变

angularjs - :first-child with ng-repeat

javascript - 将样式转换为 ng-style

javascript - 当我在代码中包含 HighChart 的 3D 图表库时,x 轴、y 轴线不出现

javascript - JavaScript 中的正则表达式仅接受带有逗号 (,) 或点 (.) 的数字

javascript - jquery html() vs empty().append() 性能

javascript - 如何使用 express req 对象获取请求路径