我是angular2和koajs的新手 . 也许这是一个愚蠢的问题 .
我有一个带有路由表的angular2应用程序,如下所示:
const appRoutes: Routes = [
{
path: 'pats/report/settings',
component: PatsReportSettingsComponent
},
{
path: 'pats/report/:id',
component: PatsReportComponent,
pathMatch: 'full'
},
{
path: 'pats/report/login',
component: PatsLoginComponent,
pathMatch: 'full'
},
{
path: '.',
redirectTo: 'pats/report/login',
pathMatch: 'full'
},
{
path: '**',
component: Pats404PageComponent,
pathMatch: 'full'
},
];
如果用户通过根URL(http://localhost:3000)访问我们的应用程序,然后导航到子页面(例如:http://localhost:3000/pats/report/settings),一切都很好,因为angular2 app将在内部处理子页面导航,并且不会向后端发出http请求 .
但是如果用户直接使用子页面URL访问我们的应用程序,例如:http://localhost:3000/pats/report/settings,则后端koajs应用程序将以404状态响应,因为它在后端不存在路径'pats/report/settings'的路径 .
What's the right way to resolve this problem?
Should I add route for all client child page paths in the backend router and respond with the index.html?
1 回答
我不知道Koajs,但你需要一些中间件 . 你的后端需要将这些请求重定向到
localhost:3000/
,但是让URL保持原样,这样角度才能发挥它的魔力!查看koajs的其他附加包:
koa-repath
rewrite
另一种选择:使用Hashbang-URL! Angular2 URL-Styles