[英]Angular/JS Express app in infinite routing loop in production mode, works fine in development mode
I have a webapp built on generator-angular-fullstack
. 我有一个基于
generator-angular-fullstack
。 It works fine in development mode ( grunt serve
), but gets stuck in what seems like an infinite routing loop when in production mode ( grunt serve:dist
). 它在开发模式(
grunt serve
)下工作良好,但是在生产模式( grunt serve:dist
)下陷入了无限路由循环。
My server console outputs this: 我的服务器控制台输出以下内容:
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
This is what my NodeJS routing code looks like: 这是我的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'
});
});
};
...and my AngularJS routing code: ...以及我的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'
});
UPDATE: Since I suspect that files are copied wrong into the /dist folder, here is the contents of /dist: 更新:由于我怀疑文件被错误地复制到/ 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
The problem most likely appears when some of the assets can't load. 当某些资产无法加载时,最有可能出现此问题。
Because instead of sending a 404 the express server in the current configuration just sends back index
, as this line in routes.js
, causing an infinite loop and crashing the browser window: 因为在当前配置中快递服务器没有发送404而是发送回
index
,这是routes.js
这一行, routes.js
导致无限循环并导致浏览器窗口崩溃:
app.get('/*', middleware.setUserCookie, index.index);
It might be just a font or an angular template that is not found. 可能只是找不到字体或有角模板。 You best bet for debugging is to change the line above to
最好的调试方法是将上面的行更改为
app.get('/', middleware.setUserCookie, index.index);
and see which http requests fail in network tab. 并在“网络”标签中查看哪些HTTP请求失败。 then make sure your
grunt build
job copies over all the required files/folders. 然后确保你的
grunt build
了所有必需的文件/文件夹拷贝工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.