【发布时间】:2018-11-09 17:27:10
【问题描述】:
我在 MEAN 堆栈应用程序上路由时遇到问题。通过 Angular 开发服务器(使用 ng serve 然后在 localhost:4200 查看)测试站点时,我的路由工作正常。但是,当我尝试仅使用为后端构建的 Express 服务器时,我遇到了一些问题。问题是,尽管站点运行良好,即使路由到运行ng build 后提供的所有静态页面,当从地址栏而不是页面上的链接重新加载或导航到页面时,我遇到了
“无法获取”错误。
路由在 Angular 生成的文件夹中的 app.module.ts 文件中定义。它看起来像这样:
const appRoutes: Routes = [
{path: '', component: HomeComponent, canActivate: [AuthGuard]}, // accessible to all
{path: 'register', component: RegisterComponent, canActivate: [AuthGuard]}, // accessible to all
{path: 'login', component: LoginComponent, canActivate: [AuthGuard]}, // accessible to all
{path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard, UserAuthGuard]}, // only accessible by users
{path: 'ridequeue', component: RidequeueComponent, canActivate: [AuthGuard, VolunteerAuthGuard]}, // Volunteers and Administrators only
{path: 'viewusers', component: ViewusersComponent, canActivate: [AuthGuard, VolunteerAuthGuard]}, // Volunteers and Administrators only
{path: 'manageusers', component: ManageusersComponent, canActivate: [AuthGuard, AdministratorAuthGuard]}, // Administrators only
{path: 'adminpanel', component: AdminpanelComponent, canActivate: [AuthGuard, AdministratorAuthGuard]}, // Administrators only
{path: 'profile', component: ProfileComponent, canActivate: [AuthGuard, UserAuthGuard]}, // only accessible by users
{path: 'feedback', component: FeedbackComponent, canActivate: [AuthGuard, UserAuthGuard]}, // only accessible by users
{path: 'unauthorized', component: UnauthorizedComponent, canActivate: [AuthGuard]} // accessible to all
];
在此文件中使用导入列表中的RouterModule.forRoot(appRoutes) 实现。
至于 Express 应用程序中的路由,我为访问的不同 API 配置了路由,并为静态站点(通过 Angular 创建的前端,我有:
app.use(express.static(path.join(__dirname, 'public')));
其他可能相关的信息:我正在使用 Express 4.16 和 CORS 中间件、Angular 6 和 Chrome 来测试它。
如何解决?
【问题讨论】:
-
这可能是help
-
第二个对我很有效@Vikas,谢谢!
标签: angular express mean-stack