简体   繁体   English

Swagger-node-express用法

[英]Swagger-node-express usage

I'd like to share my thoughts about https://github.com/swagger-api/swagger-node-express 我想分享一下我对https://github.com/swagger-api/swagger-node-express的看法

It has 564 stars on Github, so I think it is justify to use it, but: 它在Github上有564颗星,所以我认为使用它是合理的,但是:

  • Why should I use methods like addGet/addPost and split my controller by HTTP methods 为什么我应该使用像addGet / addPost这样的方法,并通过HTTP方法拆分我的控制器
  • Why should I involve models to routing? 我为什么要让模型涉及路由?
  • I can use validators for authentication, but I have to choose some paths, that should be protected there. 我可以使用验证器进行身份验证,但我必须选择一些应该受到保护的路径。 It duplicates logic , that stores in swagger json file. 它复制逻辑 ,存储在swagger json文件中。

Isn't the right way to have a static swagger.json and build Express routes on it? 是不是有一个静态swagger.json并在其上构建Express路由的正确方法? Or maybe I don't understand some practics 或者我可能不了解一些实践

Swagger is just a spec. Swagger只是一个规范。 You could hand craft a swagger spec without any code, serve the spec to a swagger-ui compliant page, and it "will work." 你可以手工制作一个没有任何代码的swagger规范,将规范提供给一个符合swigger-ui标准的页面,并且它“可以工作”。 You could have a fully defined API and "run" it using the UI pages. 您可以使用完全定义的API并使用UI页面“运行”它。 Same is true for Express without swagger - you can do a lot of neat things! 对于没有招摇的快递也是如此 - 你可以做很多整洁的事情! But some effort would be needed to document the API in swagger. 但是需要付出一些努力才能以宽广的方式记录API。

If you want to use swagger-node-express, you are coupling the code to the API documentation. 如果要使用swagger-node-express,则需要将代码与API文档相结合。 This can save you a lot of time, and keep changes in the same file, etc. However, like all your questions allude to, you must do things the swagger-node-express way. 这可以为您节省大量时间,并在同一个文件中保留更改等。但是,就像您提出的所有问题一样,您必须以swagger-node-express方式执行操作。 Faster than doing both separately, but there are constraints one must follow. 比单独做两者更快,但必须遵循一些限制。

There are other swagger packages that tackle this from different angles. 还有其他一些swagger包从不同角度解决这个问题。 I've seen some that try to build the swagger docs from the Express/Restify routes automagically. 我见过一些尝试从Express / Restify路线自动构建swagger文档。 An alternative to swagger-node-express is swagger-tools , which even includes validation, but I'd guess you'd be limited in some fashion to writing Express without any swagger integrated. swagger-node-express的另一种选择是swagger-tools ,甚至包括验证,但我猜你在某种程度上有限制写Express而没有任何swagger集成。

You are free to build swagger docs manually (or with some YAML, jsDoc package or a generator), but that would take some extra time after your routes are written. 您可以手动(或使用某些YAML,jsDoc包或生成器)自由构建swagger文档,但在写入路由后需要一些额外的时间。 Plus, it's a separate place to maintain your docs and invariably they will be out of sync at some point, if not abandoned. 另外,它是一个单独的地方来维护你的文档,如果不是放弃的话,它们总会在某些时候不同步。 Using swagger-node-express is primarily a time saver and, even with its drawbacks, that might be worth the extra development effort of the alternatives. 使用swagger-node-express主要是节省时间,即使有其缺点,也可能值得替代方案的额外开发工作。

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

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