【问题标题】:Express routing freezes site when URL has a trailing slash当 URL 带有斜杠时,快速路由会冻结站点
【发布时间】:2013-08-03 21:35:47
【问题描述】:

每当我在我的开发网站上加载一个带有localhost:8000/news 形式的 URL 的页面时,一切都会正确加载。但是,如果我添加一个斜杠,如下所示:localhost:8000/news/ 站点冻结。我可以看到它正在尝试使用路径/news/partials/footer 和其他一些加载部分。它正在尝试加载主页使用但路径错误的部分。我不知道为什么这会冻结 Chrome,但确实如此。

我已经尝试了几个小时来弄清楚这是怎么回事,但我似乎无法理解为什么会发生这种情况。这是我正在使用的代码(取出一些我保证不相关的部分):

app.js:

var app = module.exports = express();

// Configuration
app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.static(__dirname + '/public'));
    app.use(app.router);
});

app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
    app.use(express.errorHandler());
});


var galleryProvider = new GalleryProvider();

galleryProvider.client.open(function(err, result) {
  galleryProvider.client.authenticate('user', 'pass', function(err, result) {
    if (!err) {
      console.log('Mongo connected. Listening on port ' + (process.env.PORT || 8000));
      app.listen(process.env.PORT || 8000);
    } else {
      console.log(err);
    }
  });
});

// Routes
app.get('/', routes.index);
app.get('partials/:name', routes.partials);

/* some backend routes that the page does not look at */

app.get('*', routes.index);

路由/index.js

/*
 * GET home page.
 */

//get correct directory path
var filePath = __dirname.replace('routes', 'views/');

exports.index = function(req, res) {
    res.sendfile(filePath + 'index.html');
};

exports.partials = function (req, res) {
    var name = req.params.name;
    res.sendfile(filePath + 'partials/' + name + '.html');
};

public/js/app.js

// Declare app level module which depends on filters, and services
var lscGallery = angular.module('lscGallery', []);

lscGallery
  .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/', {templateUrl: 'partials/home', controller: 'AppCtrl'});
    $routeProvider.when('/artists', {templateUrl: 'partials/artists', controller: 'GalleryCtrl'});
    $routeProvider.when('/exhibitions', {templateUrl: 'partials/exhibitions', controller: 'GalleryCtrl'});
    $routeProvider.when('/news', {templateUrl: 'partials/news'});
    $routeProvider.when('/about', {templateUrl: 'partials/about'});
    $routeProvider.when('/contact', {templateUrl: 'partials/contact'});
    $routeProvider.when('/admin', {templateUrl: 'partials/login'});
    $routeProvider.otherwise({redirectTo: '/'});

    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('!');
  }]);

谢谢!

附:如果有帮助,我将使用 this 作为我网站的种子。

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    我想通了。我认为在美好的一天坐在外面才能弄清楚。

    部分路由仍然在我的指令中设置为相对路径(例如templateUrl: 'partials/gallery'),即使我想始终从根 url 中提取部分。傻我。

    【讨论】:

      猜你喜欢
      • 2019-01-03
      • 2015-09-07
      • 1970-01-01
      • 2018-03-30
      • 2020-03-17
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      相关资源
      最近更新 更多