Nowadays REST APIs plays important role in every applications, Where it has been used to connect with other applications and to share data with that applications. REST APIs without a good documentation on how to use it, is useless. Because of that, developers have worked hard to create a standard way of describing APIs and documentation. Swagger On Node.js providing a reliable solution for design, development, testing and documentation of REST APIs.
Swagger is a powerful API developer tools for teams and individuals, enabling development across the entire API lifecycle, from design and documentation, to test and deployment. Swagger consists of a mix of open source, free and commercially available tools that allow anyone, from technical engineers to street smart product managers to build amazing APIs that everyone loves. For Building APIs With Node.js, Swagger provides official module for designing and building Swagger-compliant APIs entirely in Node.js. It integrates with popular Node.js servers, including Express, Hapi, Restify, and Sails, as well as any Connect-based middleware.
Swagger On Node.js Using Express
- Install Swagger package globally, Using the command
npm install -g swagger
While installation you will be prompted for framework, Choose express
- Create a new swagger project using CLI with command
swagger project create hello-world
- You will be created with swagger boilerplate project with directory structure as below,
apicontains everything related to the API development(controllers, mocks, helpers and swagger).
~/swaggerdirectory contains information of routes and its documentations.
configcontains default.yaml that, as the documentation states, drives the application’s config directory.
testto test for REST APIs.
app.jsis the main file which runs the server
- You can edit your routes directly at
~/app/swaggeror use interactive, browser-based Swagger Editor is built in. It provides Swagger 2.0 validation and endpoint routing, generates docs on the fly, and consumes easy-to-read YAML with command.
swagger project edit
- Code your API’s business logic in Node.js at controllers (
~/api/controllers). All route will be linked with the controller with
x-swagger-router-controllerelement at routes, Which specifies the name of the controller file associated with the
/hellopath. So, the controller source file for this project is
operationIdelement specifies which controller function to call, it is a function called hello.
- You can run your project server, Using command
swagger project start
Now, Your application is build with Swagger, Which make your application API design, development, documentation and maintenance work easier. If you have any comments/suggestions/feedback leave on below comment box.