AngularJS and nodeJs Express: routes and refresh

假如想象 提交于 2019-12-01 12:16:33

This is a problem commonly encountered when using a "catch-all" route that returns index.html (which you have to do when using Angular's html5 mode).

I don't know what your public folder structure looks like, but your Angular is making a request for activate.html that doesn't match its actual location (as served by Express). It may be because you have a leading slash in your templateUrl:

{ templateUrl: '/page/activate.html'})

but that depends on what your public folder looks like. "/page/activate.html" assumes that the "page" folder is at the root of your public folder. If it isn't, then that's your problem. You should have:

{ templateUrl: 'path/relative/to/public/folder/page/activate.html'})

which I'm guessing in your case would be something like:

{ templateUrl: 'views/page/activate.html'})

(It doesn't seem likely there would be a folder called "page" at the root of public/.)

Your express middleware attempts to handle the request for

http://localhost:1234/page/activate.html

but nothing successfully handles it, so your catch-all route just returns index.html, which has multiple root elements, causing Angular to throw a fit. Angular expected activate.html but it got index.html instead.

This will happen whenever the client makes a request that Express can't handle, because the catch-all router will return index.html. You can get your browser in an infinite loop as it continuously loads index.html, which loads Angular, which (erroneously) loads index.html, which loads Angular, etc.

Look at the request your browser is making for activate.html and make it match the actual location (as served by Express) of activate.html.

Try use the specify the templateUrl with the absolute path from the root of the views.

For example, suppose you have configured and views is in the public folder

app.set('views', __dirname + '/public');

then change the path to the template to

/public/page/home.html
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!