10

So I've documented my whole API with swagger editor, and now I have my .yaml file. I'm really confused how I take that and generate the whole nodejs stuff now so that all those functions are already defined and then I just fill them in with the appropriate code.

Gargoyle
  • 9,590
  • 16
  • 80
  • 145

1 Answers1

12

Swagger Codegen generates server stubs and client SDKs for a variety of languages and frameworks, including Node.js.

To generate a Node.js server stub, run codegen with the -l nodejs-server argument.

Windows example:

java -jar swagger-codegen-cli-2-2-2.jar generate -i petstore.yaml -l nodejs-server -o .\PetstoreServer

You get:

.
├── api
|    └── swagger.yaml
├── controllers
|    ├── Pet.js
|    ├── PetService.js
|    ├── Store.js
|    ├── StoreService.js
|    ├── User.js
|    └── UserService.js
├── index.js
├── package.json
├── README.md
└── .swagger-codegen-ignore
Helen
  • 87,344
  • 17
  • 243
  • 314
  • Thanks, that's great! Is there a way to generate it using typescript instead of just plain javascript? – Gargoyle Apr 20 '17 at 15:37
  • @Gargoyle: There are TypeScript generators too, try `-l typescript-node`. – Helen Apr 20 '17 at 16:06
  • That looks like it is generating a client side, not a server side. – Gargoyle Apr 21 '17 at 04:32
  • 1
    @Gargoyle: My bad. Looks like Swagger Codegen does not have a TypeScript server generator currently. [This answer](http://stackoverflow.com/a/36681516/113116) mentions the [swagger-node](https://github.com/swagger-api/swagger-node) project as a possible alternative. Also check out the [list of Node.js integrations](http://swagger.io/open-source-integrations/#node-js-15) on http://swagger.io. – Helen Apr 21 '17 at 09:24