【问题标题】:MEAN Stack routing issue, angular routing does not work平均堆栈路由问题,角度路由不起作用
【发布时间】:2015-07-18 01:27:26
【问题描述】:

我有我的 routes.js

module.exports = function(app) {
//I can't use this because angular routing does not work
/*app.get('*', function(req, res) {
    res.sendfile('./public/index.html');
});*/
    app.get('/submit', function(req,res){
        res.sendfile('./public/submit.html');
    }); 

    app.get('/schedule', function(req,res){
        res.sendfile('./public/schedule.html');
    }); 

    app.get('/requests', function(req,res){
        res.sendfile('./public/requests.html');
    });

    app.get('/tv_left', function(req,res){
        res.sendfile('./public/tv_left.html');
    });

    app.get('/tv_center', function(req,res){
        res.sendfile('./public/tv_center.html');
    });

    app.get('/tv_right', function(req,res){
        res.sendfile('./public/tv_right.html');
    });

    app.get('/', function(req, res){
        res.sendfile('./public/index.html');
    });
};

和我的 appRoutes.js 一样

angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    $routeProvider

        // home page
        .when('/', {
            templateUrl: 'index.html',
            controller: 'LoginController'
        })

        .when('/submit', {
            templateUrl: 'submit.html',
            controller: 'SubmitController'
        });

    $locationProvider.html5Mode(true);

}]);

基本上,如果我使用 app.get('*'),那么任何请求都会返回到 index.html,即使 url 发生了变化。

【问题讨论】:

  • 您遇到错误了吗?请详细说明什么不起作用的核心问题
  • 使用资产目录要聪明得多,然后将不是目录的任何内容路由到索引
  • @charlietfl 你是什么意思?

标签: javascript angularjs express mean-stack


【解决方案1】:

这是因为 express 按照定义的顺序处理路由。如果您希望 index.html 作为一个包罗万象的路由,请将其移至函数的底部。

延伸阅读:https://www.safaribooksonline.com/blog/2014/03/10/express-js-middleware-demystified/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2015-04-12
    • 2017-06-22
    • 2010-10-15
    • 2014-11-10
    相关资源
    最近更新 更多