【发布时间】:2019-02-16 13:16:53
【问题描述】:
假设这是在带有 Angular 前端的 Node 服务器上处理所有路由的部分。当用户输入站点 URL 时,服务器会发送所有静态文件供浏览器呈现。但是,我希望客户端处理某些路由,而不是直接进入服务器。
例如,如果我输入 www.exemple.com/page2,则会向服务器发送一个 Get 请求,但该路由不存在,因此该请求只是挂在那里并最终导致错误。
我希望 Angular 处理路由,而不是自动发送到服务器。我只是成功地让它在 localhost 上工作,其中 Angular 应用程序从与服务器侦听的端口不同的端口提供服务。谁能告诉我如何实现这一目标?非常感谢。
module.exports=function(app,dir){
app.use(express.json());
app.use(express.static(dir+'/dist/probeeshq'));
app.get('/',(req,res)=>{res.sendFile(path.join(dir));});
app.use('/auth', userAuth);
app.use('/api/me',userData);
app.use('/api/org',organization);
app.use('/api/messaging',messaging);
app.use('/api/search',search);
app.use(error);
}
这就是我在 Angular 中所拥有的
const routes: Routes = [
{ path:'', component: HomeComponent },
{ path:'project_gaea', component:ProjectGaeaComponent},
{ path:'dashboard', component: DashboardComponent ,canActivate:[AuthGuardService]},
{ path:'explore', component:ExploreComponent, canActivate:[AuthGuardService]},
{ path:'create', component: CreateComponent },
{ path:'user/:id', component:UserProfileComponent},
{ path:'**', component: PageNotFoundComponent}
];
【问题讨论】:
标签: node.js angular express routing