路线建筑的最佳实践

发布于 2025-01-31 10:59:50 字数 1096 浏览 1 评论 0原文

我正在尝试为后端设置路线。我有两种方法试过设置这些路线,我想知道哪种方式适合最佳实践(或者都不适合?)。差异很小,但是我很想知道这里是否有一个客观的“最好”。

以下是我的尝试:

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');

router.get('/', flashcardController.readFlashcard);
router.post('/', flashcardController.createFlashcard);
router.patch('/', flashcardController.updateFlashcard);
router.delete('/', flashcardController.deleteFlashcard);

module.exports = router

当然

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');


module.exports = (app) => {
    router.get('/api/flashcard', flashcardController.readFlashcard);
    router.post('/api/flashcard', flashcardController.createFlashcard);
    router.patch('/api/flashcard', flashcardController.updateFlashcard);
    router.delete('/api/flashcard', flashcardController.deleteFlashcard);

    app.use('/', router);
};

,我的app.js(我的后端的入口点)文件将需要针对每个选项中的每个选项略有不同。

I'm trying to set up routes for my backend. I have two ways that I've tried setting these routes up, and I'm wondering which way fits best practices (or neither?). The differences are minimal, but I'd love to know if there is an objective "best" here.

Here are my attempts:

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');

router.get('/', flashcardController.readFlashcard);
router.post('/', flashcardController.createFlashcard);
router.patch('/', flashcardController.updateFlashcard);
router.delete('/', flashcardController.deleteFlashcard);

module.exports = router

VS

const express = require("express");
const router = express.Router();
const flashcardController = require('../controllers/flashcardController');


module.exports = (app) => {
    router.get('/api/flashcard', flashcardController.readFlashcard);
    router.post('/api/flashcard', flashcardController.createFlashcard);
    router.patch('/api/flashcard', flashcardController.updateFlashcard);
    router.delete('/api/flashcard', flashcardController.deleteFlashcard);

    app.use('/', router);
};

Of course, my app.js (entry-point for my backend) file will need to be coded slightly differently for each of these options.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2025-02-07 10:59:50

如果您认为路由器的工作就是处理它收到的一些请求,而这是通话代码的工作,将路由器放置在呼叫代码希望其操作的任何路径上,那么只有您的第一个选项才能做到那。这将允许呼叫者在所需的任何路径上使用这些路线。

如果您希望实现路由完全自给自足并将路由安装在希望它们打开的路径上的模块,则只有第二个选项可以。

我要说的是,“通常”和更多的“灵活”方案是呼叫者将路线放在需要它们的路径上的第一个方案。但是,您可以自由选择想要的哪种样式。

第二种选择不是特别有效地实施,因此可以改进。根本不需要路由器,因为可以直接将路由直接安装在App对象上。并且,可以避免多次重复/api/flashcard

例如,第二个选项可能是:

const controller = require('../controllers/flashcardController');
const routePath = '/api/flashcard';

module.exports = (app) => {
    app.get(routePath, controller.readFlashcard);
    app.post(routePath, controller.createFlashcard);
    app.patch(routePath, controller.updateFlashcard);
    app.delete(routePath, controller.deleteFlashcard);
};

或者甚至是这样:

const controller = require('../controllers/flashcardController');

module.exports = (app) => {
    app.route('/api/flashcard')
        .get(controller.readFlashcard)
        .post(controller.createFlashcard)
        .patch(controller.updateFlashcard)
        .delete(controller.deleteFlashcard);
};

并且,第一个选项可以简化为:

const router = require("express").Router();
const controller = require('../controllers/flashcardController');

router.route('/')
  .get(controller.readFlashcard)
  .post(controller.createFlashcard)
  .patch(controller.updateFlashcard)
  .delete(controller.deleteFlashcard);

module.exports = router

If you believe that the job of a router is to just handle some requests that it receives and it is the job of the calling code to place the router at whatever path the calling code wants it to operate on, then only your first option will do that. This would allow a caller to use these routes in whatever path it wants.

If you want the module that implements the routes to be entirely self-sufficient and install the routes on the path it wants them to be on, then only the second option does that.

I would say that the "usual" and more "flexible" scheme is the first one where the caller places the routes on the path where it wants them. But, you are free to choose whichever style you want.

The second option is not implemented particularly efficiently so it could be improved. No router is needed at all as the routes can be just installed directly on the app object directly. And, repeating /api/flashcard multiple times can be avoided.

For example, the second option could be this:

const controller = require('../controllers/flashcardController');
const routePath = '/api/flashcard';

module.exports = (app) => {
    app.get(routePath, controller.readFlashcard);
    app.post(routePath, controller.createFlashcard);
    app.patch(routePath, controller.updateFlashcard);
    app.delete(routePath, controller.deleteFlashcard);
};

Or, even just this:

const controller = require('../controllers/flashcardController');

module.exports = (app) => {
    app.route('/api/flashcard')
        .get(controller.readFlashcard)
        .post(controller.createFlashcard)
        .patch(controller.updateFlashcard)
        .delete(controller.deleteFlashcard);
};

And, the first one could be simplified to this:

const router = require("express").Router();
const controller = require('../controllers/flashcardController');

router.route('/')
  .get(controller.readFlashcard)
  .post(controller.createFlashcard)
  .patch(controller.updateFlashcard)
  .delete(controller.deleteFlashcard);

module.exports = router
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文