路线建筑的最佳实践
我正在尝试为后端设置路线。我有两种方法试过设置这些路线,我想知道哪种方式适合最佳实践(或者都不适合?)。差异很小,但是我很想知道这里是否有一个客观的“最好”。
以下是我的尝试:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您认为路由器的工作就是处理它收到的一些请求,而这是通话代码的工作,将路由器放置在呼叫代码希望其操作的任何路径上,那么只有您的第一个选项才能做到那。这将允许呼叫者在所需的任何路径上使用这些路线。
如果您希望实现路由完全自给自足并将路由安装在希望它们打开的路径上的模块,则只有第二个选项可以。
我要说的是,“通常”和更多的“灵活”方案是呼叫者将路线放在需要它们的路径上的第一个方案。但是,您可以自由选择想要的哪种样式。
第二种选择不是特别有效地实施,因此可以改进。根本不需要路由器,因为可以直接将路由直接安装在
App
对象上。并且,可以避免多次重复/api/flashcard
。例如,第二个选项可能是:
或者甚至是这样:
并且,第一个选项可以简化为:
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:
Or, even just this:
And, the first one could be simplified to this: