【问题标题】:Adding Routes to ExpressJS App from Modules从模块向 ExpressJS 应用程序添加路由
【发布时间】:2020-11-06 03:29:11
【问题描述】:

试图弄清楚路由中间件是如何在 ExpressJS 上工作的。

我想要解决的是: app 在 /index.js 中有一些基本的路由

应用程序将有一些模块来添加自己的路由和处理程序(例如 hello 和 world)

我添加了不同的路由选项以查看哪些有效 - 但我煮过头了。

https://codesandbox.io/s/flamboyant-mcnulty-7b79x

【问题讨论】:

    标签: node.js express routes


    【解决方案1】:

    像这样添加hello 和world 路由:

    app.use(hello());
    world.setup(app);
    

    由于某种原因,connect-history-api-fallback 模块将导致 nothing matched 路由。

    如果你注释代码使用history中间件:

    // app.use(history());
    

    现在,您可以通过https://ncpt4.sse.codesandbox.io/hello 访问hello 路由,并通过https://ncpt4.sse.codesandbox.io/world 访问world 路由。

    示例链接:https://codesandbox.io/s/amazing-worker-ncpt4

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 2022-07-12
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多