Html5模式Angularjs Express URL重写返回所有请求的索引页

Html5模式Angularjs Express URL重写返回所有请求的索引页,html,angularjs,express,Html,Angularjs,Express,我一直在使用类似的问题,试图找到解决问题的方法。我知道为了在angularjs中使用HTML5模式,我需要告诉服务器如何处理对页面的直接访问 我有一个问题,点击应用程序中的链接会使页面变好,但直接访问不会显示优惠 例如 应该调用routes.js app.get('/offers', offers.all); //Offer Routes var offers = require('../app/controllers/offers'); app.get('/offers', offers.

我一直在使用类似的问题,试图找到解决问题的方法。我知道为了在angularjs中使用HTML5模式,我需要告诉服务器如何处理对页面的直接访问

我有一个问题,点击应用程序中的链接会使页面变好,但直接访问不会显示优惠

例如

应该调用routes.js

app.get('/offers', offers.all); 
//Offer Routes
var offers = require('../app/controllers/offers');
app.get('/offers', offers.all); 
app.get('/offers/:offerId', offers.show);

//Home route
var index = require('../app/controllers/index');
app.get('/', index.render);
当链接

  <a href="offers/television">televisions</a>
我的index.jade head中也有base(href=“/”)

angular config.js

//Setting up route
window.app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/offers/:type/',{
        controller: 'OffersController',
        templateUrl: 'views/offers/list.html'
      }).
      when('/', {
        templateUrl: 'views/index2.html'
      }).
      otherwise({
        redirectTo: '/'
      });
    }
 ]);
 //Setting HTML5 Location Mode
 window.app.config(['$locationProvider',
   function($locationProvider) {
     $locationProvider.hashPrefix("!");
     $locationProvider.html5Mode(true)
   }
 ]);
express routes.js

app.get('/offers', offers.all); 
//Offer Routes
var offers = require('../app/controllers/offers');
app.get('/offers', offers.all); 
app.get('/offers/:offerId', offers.show);

//Home route
var index = require('../app/controllers/index');
app.get('/', index.render);
express.js

    app.configure(function() {
    // app.use('/', express.static(__dirname + '/'));

    //cookieParser should be above session
    app.use(express.cookieParser());

    //bodyParser should be above methodOverride
    app.use(express.bodyParser());
    app.use(express.methodOverride());


    //routes should be at the last
    app.use(app.router);

    app.get('/*', function(req, res) {
            res.render('index');
    });
 ...
为什么即使它应该点击express routes.js中的/offers路径,它也不返回优惠?还是我在做些奇怪的事


谢谢

正如您在问题的评论中提到的,
“app.get('/offers',offers.all);将处理/offers/:offerId”
。这意味着直接转到
http://localhost:3001/offers/television/
将由您的
提供处理。所有
函数(未在post中显示),而不是返回索引的
'/*'
处理程序

要解决这个问题,你有很多选择

  • 检查路由以查看它是否是AJAX请求。如果是,, 返回数据,如果不是,则返回索引
  • 将API放在路径后面(如/API),然后所有API请求都将转到
    /api/offers/:offId
    获取数据。这将释放出
    /offers/:offerId
    将由
    '/*'
    处理,返回索引
  • 编辑:
    我看到了混乱,
    app.router
    ()。简而言之,
    app.use(app.router)告诉express以哪个顺序整体运行路由。在该点之后,您提供路线的顺序将起作用。从您的代码(同样,并没有显示所有内容)中,您实际上只定义了一个路由,
    app.get('/*',function(req,res){res.render('index');})。您有单独的路由文件,但在您发布的内容中没有包含这些脚本的位置。默认情况下,它们不是自动包含的。

    需要更多代码。这是一项工厂服务,但我不知道它在哪里/如何使用。我看不到你有棱角的路线。另外,我在express here.app.get('/offers',offers.all)中没有看到
    /offers/:offerId
    的路线;will handle/offers/:offerId:offerId仅在参数为时添加。将添加更多的代码。你能检查一下这个url吗?也检查一下:这是我最初认为正在发生的事情。实际发生的情况是当我包括app.get('/*',function(req,res){res.render('index');});它并没有带来报价,但当我把它评论出来时,它确实带来了。因此,这是匹配/提供并呈现索引,即使它位于app.use(app.router)Does
    app.get('/*',function(req,res){res.render('index');})出现在app.get('/offers',offers.all)之前或之后?您需要发布整个内容,或者至少更大的部分。订单问题。在上面添加了更多代码…app.router出现在app.get('/*')的上方,但当angular requests/offers/Your edit正确时,这肯定会被调用并呈现索引页,我想app.router告诉express查看routes.js。谢谢你的帮助。