Node.js 防止Angular 6路由器覆盖Express Server中定义的路由
如何防止角度路由与来自Express Node服务器的路由发生冲突 我正在Express服务器中设置一些路由(到节点红色中间件): server.jsNode.js 防止Angular 6路由器覆盖Express Server中定义的路由,node.js,angular,express,webpack,angular2-routing,Node.js,Angular,Express,Webpack,Angular2 Routing,如何防止角度路由与来自Express Node服务器的路由发生冲突 我正在Express服务器中设置一些路由(到节点红色中间件): server.js // Serve the editor UI from /red app.use(settings.httpAdminRoot,RED.httpAdmin); // Serve the http nodes UI from /api app.use(settings.httpNodeRoot,RED.httpNode); // Ang
// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);
// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);
// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));
// Send all other requests to the Angular app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.js'));
});
但是在我的路由器模块中,它们总是被覆盖(使用我们的路由器而不使用默认路径重定向)
路由.module.ts
const appRoutes: Routes = [
{
path: "example-page",
loadChildren: "../modules/example/example.module#ExampleModule?sync=true"
},
// Last routing path specifies default path for application entry
/* {
path: "**",
redirectTo: "/example-page",
pathMatch: "full"
},*/
];
我之所以只提供这一小部分代码,是因为我想问,一般来说,如何防止Angular与Express server中定义的路由发生冲突,以及在Express/Node.js+Angular+AspNetCore+Webpack应用程序中路由的最佳实践是什么。如果使用Angular,那么让Angular处理所有页面。这段代码会处理这些问题,因此angular将处理所有路由
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.js'));
});
如果希望express处理某些路线而不是角度路线,请在处理角度路线之前先处理该路线,如下所示:
app.get('/some-non-angular-path', (req, res) => {
//code to handle this path. once it is handled here, angular won't be involved
//non-angular-paths should come first in the order
});
app.get((req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.js'));
});
谢谢你的回复!我也这么认为,因此我将“app.use(settings.httpAdmin,RED.httpAdmin)”部分放在角度路由处理部分之前。但也许出于某种原因,app.use在这里不起作用。我试试看,然后接受你的回答;)很抱歉迟了答复。不幸的是,没有!你的答案很有道理,可能会帮助其他人。因此,如果没有其他人有任何想法,我会接受它,让它帮助其他人。顺便说一句,如果你试图在角度路由中路由到某个非角度路径,那么它将不起作用。您必须通过提供完整路径来重定向,例如说
window.location.href=http://localhost:3000/some-非角度路径“
单击链接/按钮。这样,它将到达服务器端的app.js,相应的路由将被处理,而不是角度。当我尝试的时候,它起了作用。