【问题标题】:How to set the path for models routes in Node and Express如何在 Node 和 Express 中设置模型路由的路径
【发布时间】:2021-08-16 15:15:19
【问题描述】:

我有一个导出路线的模型文件

const newsRoutes = (app: express.Application): void => {
  app.get('/news', readAll);
  app.get('/news/:id', readOne);
};

然后,我正在导入并调用传递已初始化应用实例的函数

newsRoutes(app);

我可以通过以下方式访问它:

  • http://localhost:8080/users
  • http://localhost:8080/users/1

我想设置路径,以便在模型文件中我可以使用/ 而不是/news/:id 而不是/news/:id,并且在前面加上/api 路径,以便生成端点 会是

  • http://localhost:8080/api/users
  • http://localhost:8080/api/users/1

我尝试使用 app.use() 在服务器文件中传递路径和 newsRoutes(),但没有成功。

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    使用

    const userRoutes = require('../Routes/UserRoutes');
    app.get('User',userRoutes);
    

    同样,也可以使用模型和帮助文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 2022-01-04
      • 2013-08-22
      • 2017-01-03
      • 2018-01-10
      • 2014-03-04
      • 2020-10-05
      • 1970-01-01
      相关资源
      最近更新 更多