【问题标题】:Angular/JS Express app in infinite routing loop in production mode, works fine in development modeAngular/JS Express 应用程序在生产模式下无限路由循环,在开发模式下工作正常
【发布时间】:2014-02-07 09:02:23
【问题描述】:

我有一个基于generator-angular-fullstack 的网络应用程序。它在开发模式 (grunt serve) 下运行良好,但在生产模式 (grunt serve:dist) 时卡在看似无限的路由循环中。

我的服务器控制台输出这个:

Express server running on http://localhost:9000 in production mode
finished populating things
finished populating users
GET / 200 39ms - 1.41kb
GET /views/ui-editor/editor.html 200 15ms - 1.41kb

这就是我的 NodeJS 路由代码的样子:

module.exports = function(app) {

    // Server API Routes
    app.get('/api/awesomeThings', api.awesomeThings);

    app.post('/api/users', users.create);
    app.put('/api/users', users.changePassword);
    app.get('/api/users/me', users.me);
    app.get('/api/users/:id', users.show);

    app.post('/api/session', session.login);
    app.del('/api/session', session.logout);

    // All other routes to use Angular routing in app/scripts/app.js
    // TODO: redo so won't list all subfolders like this: http://stackoverflow.com/questions/18553847/express-angular-routing-causing-infinite-loop-crash
    app.get('/partials/*', index.partials);
    app.get('/common/*', index.partials);
    app.get('/ui-editor/*', index.partials);
    app.get('/*', middleware.setUserCookie, index.index);

    app.get('/:session', function(req, res) {
        res.render('views/index.html', {
            title: 'My App'
        });
    });

};

...和我的 AngularJS 路由代码:

$routeProvider
    .when('/main', {
        templateUrl: 'partials/main',
        controller: 'MainCtrl'
    })
    .when('/login', {
        templateUrl: 'partials/login',
        controller: 'LoginCtrl'
    })
    .when('/signup', {
        templateUrl: 'partials/signup',
        controller: 'SignupCtrl'
    })
    .when('/settings', {
        templateUrl: 'partials/settings',
        controller: 'SettingsCtrl',
        authenticate: true
    })
    .when('/:session', {
        templateUrl: 'views/ui-editor/editor.html',
        controller: 'weldEditorController'
    })
    .otherwise({
        redirectTo: '/my-project'
    });

更新:由于我怀疑文件被错误地复制到 /dist 文件夹中,这里是 /dist 的内容:

|-/.git (files inside)
|-/lib
|---config
|-----config.js
|-----dummydata.js
|-----env
|-------all.js
|-------development.js
|-------production.js
|-------test.js
|-----express.js
|-----passport.js
|---controllers
|-----api.js
|-----index.js
|-----session.js
|-----users.js
|---middleware.js
|---models
|-----thing.js
|-----user.js
|---routes.js
|---socket.js
|-/package.json
|-/Procfile
|-/public
|---bower_components (files inside)
|---images (files inside)
|---scripts
|-----c9afc898.vendor.js
|-----e4b45689.scripts.js
|---styles
|-----a5896f90.main.css
|-/server.js
|-/views
|---404.html
|---index.html
|---partials
|-----login.html
|-----main.html
|-----navbar.html
|-----settings.html
|-----signup.html
|---ui-editor
|-----editor.html
|-----weldPropertiesPanel.html

【问题讨论】:

  • 浏览器控制台有输出吗?
  • @AndreyShustariov:在我杀死服务器之前什么都没有——然后它会为客户端 JS 文件发出一些 GET 请求。
  • 查看附加的文件/目录树。
  • 是什么让你觉得这是一个无限路由循环问题? editor.html 视图是否被渲染?
  • @AndreyShustariov 我猜想由于 Chrome 状态栏在“等待服务器...”和“等待缓存...”之间切换,最终挂起。不,editor.html 不会渲染。

标签: javascript node.js angularjs express


【解决方案1】:

问题很可能出现在某些资产无法加载时。

因为当前配置中的 express 服务器没有发送 404,而是返回 index,就像 routes.js 中的这一行一样,导致无限循环并崩溃浏览器窗口:

app.get('/*', middleware.setUserCookie, index.index);

可能只是找不到字体或角度模板。 您最好的调试方法是将上面的行更改为

app.get('/', middleware.setUserCookie, index.index);

并查看网络选项卡中哪些 http 请求失败。 然后确保您的 grunt build 作业复制所有必需的文件/文件夹。

【讨论】:

  • 这是很久以前的事了,但我认为这解决了它。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
相关资源
最近更新 更多