假设这是所有路由都在具有 Angular 前端的 Node 服务器上处理的部分。当用户输入站点 URL 时,服务器会发送所有静态文件供浏览器呈现。但是,我希望客户端处理某些路由,而不是直接发送到服务器。
例如,如果我输入 www.exemple.com/page2,则会向服务器发送 Get 请求,但路由不存在,因此该请求只是卡在那里并最终导致错误。
我希望 Angular 处理路由,而不是自动发送到服务器。我只是成功地让它在本地主机上工作,其中 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}
];
最佳答案
您可以通过实现 Angular 开箱即用的路由功能来实现此目的。实现此功能后,您就可以将后端用作 API。
关于node.js - 如何将路由控制从Node服务器传递给客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52286001/