简体   繁体   English

ExpressJS,路由定义,控制器位置和api文档

[英]ExpressJS, route definition, controller location and api documentation

Using expressJS 4.X with nodeJS 6.x 将expressJS 4.X与nodeJS 6.x一起使用

I was previously defining my routes this way : 我以前是这样定义路线的:

/**
 * @api {get} /users/:userId Get a user
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Integer} userId Users unique ID.
 *
 * @apiSuccess (Success 201) {text} User email
 * @apiError {text} 401/Unauthorized.
 * @apiError {text} 404/Not Foud Unknown userId
 */
router.get('/users/:userId', function(req, res, next) {
    const userId = req.params.userId;
    //get user  
    res.json(user);
}

I found it was the correct way to do it because : 我发现这是正确的方法,因为:

  • You write the route documentation above the route definition 您在路线定义上方编写了路线文档
    • If you modify the route, you modify the documentation 如果修改路线,则修改文档
  • You have the route documentation above your controller 您在控制器上方有路线文档
    • URL params/body content (req.params.name // req.body.name) URL参数/正文内容(req.params.name // req.body.name)
    • HTTP Error code to return 返回的HTTP错误代码
    • IDE like webstorm use those comment for autocompletion 像webstorm这样的IDE使用这些注释进行自动补全

Looking for best practices, I've been told many times I should create a Controller and make the route defintion elsewhere, ending with the following code : 在寻找最佳实践时,已经多次告诉我应该创建一个Controller并在其他地方进行路由定义,并以以下代码结尾:

class UserController {
    constructor() {
        this.listAll = this.listAll.bind(this);
    }
    getUser(req, res, next) {
        const userId = req.params.userId;
        //get user...
        res.json(user);
    }
}
router.get('/users/, UserController.getUser);

The only good reason I see with this way of organising your code is that if you have 2 roads doing the same stuff, you can make them use the same controller. 我用这种组织代码的方式看到的唯一好的理由是,如果有两条路在做相同的事情,则可以使它们使用相同的控制器。

  • Should I keep seperating my controller & my routes ? 我应该继续分隔控制器和路线吗?
  • If yes, how should I document it ? 如果是,我应该如何记录?
  • What are the benefits of such a code organisation ? 这样的代码组织有什么好处?

Bit of a philosophical question which should be asked rather on the http://programmers.stackexchange.com page. 应该在http://programmers.stackexchange.com页面上问一些哲学问题。 But in any case... 但是无论如何

My personal approach when I use a framework is to follow the style of the framework itself, and never change the coding style. 使用框架时,我的个人方法是遵循框架本身的样式,而不更改编码样式。 To me this is important especially if I work with other developers. 对我来说,这很重要,特别是如果我与其他开发人员一起工作。

Let say you want to bring someone new to the team. 假设您想将某个新成员带入团队。 You can't require any more ExpressJS experience since you change the way the code is structured. 因为您更改了代码的结构方式,所以不再需要ExpressJS经验。 Meaning you'll have to sit down with the new person and explain the different coding style. 这意味着您将不得不与新人坐下来,并解释不同的编码风格。

Another thing is that turning everything in to a class is an overkill. 另一件事是,将所有内容放到一堂课上是太过分了。 It is an extra layer of unnecessary complexity, that you and other will have to wrap your heads around it. 这是不必要的复杂性的额外一层,您和其他人将不得不把头围起来。 In addition you won't be using the benefits of the class in this case. 另外,在这种情况下,您将不会使用该类的好处。

If it was me, I would keep the coding style as ExpressJS intended it to be, and as simple as possible :). 如果是我,我将保持ExpressJS所希望的编码风格,并尽可能简单:)。 Comments? 评论? Each route with a nice explanation like this for example: 每条路线都有一个很好的解释,例如:

/**
 * Nice description
 *
 * @param {string}   var-name   description
 * @param {int} var-name   description
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM