【问题标题】:Is there a way to override Angular router using Node.js routing - Just for a specific path?有没有办法使用 Node.js 路由覆盖 Angular 路由器 - 仅针对特定路径?
【发布时间】:2019-02-19 19:45:34
【问题描述】:

我目前有一个通过 Node 向用户提供的服务,并且正在使用 Angular 的路由器。
一切都很好,除了一件事:我想将 Node 的路由器用于一个特定的路径,而不是 Angular 的路由器。 如何做到这一点?

作为临时解决方案,我一直在尝试使用CORS,在某种程度上是成功的,但肯定不是可以使用的解决方案。

我看到了这个 Stackoverflow 帖子:
MEAN - Angular routes over-riding NodeJS routes

但它似乎主要针对 Angular 的路由器。

特定路径的传入请求(例如:对“/specificpath”的 POST 请求)应由 Node.js 处理,其余请求应由 Angular 的路由器处理。

谢谢。

【问题讨论】:

    标签: html node.js angular exception router


    【解决方案1】:

    通常,您将所有传入流量重定向到 index.html 文件,以便 Angular 负责路由:

    app.get('*', function (req, res) {
        res.sendFile('/public/index.html');
    });
    

    由于这被视为已注册的中间件,因此您可以通过在 node.js 中通常执行的方式指定路由来简单地添加不应发送到 Angular 的路由:

    app.get('/specificpath', yourController.yourMethod);
    

    所以完整的代码示例是:

    app.get('/specificpath', yourController.yourMethod);
    app.get('*', function (req, res) {
        res.sendFile('/public/index.html');
    });
    

    【讨论】:

    • 谢谢朱利安。这将首先定义特定路径,然后是下面的静态中间件吗? app.use(express.static('dist/'))
    • 我想是的,试试吧。我无法测试它,抱歉
    猜你喜欢
    • 2018-04-22
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    相关资源
    最近更新 更多