UNPKG

5.62 kBMarkdownView Raw
1The `swagger` module provides tools 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. With `swagger`, you can specify, build, and test your API from the very beginning, on your laptop. It allows you to change and iterate your design without rewriting the logic of your implementation.
2
3
4Remember, one great thing about this approach is that all of the Swagger validation logic is handled for you, and all of the routing logic is managed through the Swagger configuration. You don't have to code (or recode!) any of that stuff yourself.
5
6# Your swagger API in five steps
7
8## 1. Install the swagger module
9
10Install using npm. For complete instructions, see the [install](./docs/install.md) page.
11
12```bash
13$ npm install -g dukeswag
14```
15
16## 2. Create a new swagger project
17
18Use the [CLI](./docs/cli.md) to create and manage projects. Learn more on the [quick start](./docs/quick-start.md) page.
19
20```bash
21$ swagger project create hello-world
22```
23
24## 3. Design your API in the Swagger Editor
25
26The interactive, browser-based [Swagger Editor](http://editor.swagger.io/) is built in. It provides Swagger 2.0 validation and endpoint routing, generates docs on the fly, and consumes easy-to-read YAML.
27
28```bash
29$ swagger project edit
30```
31
32## 4. Generate controllers for your API
33
34The platform allows you to generate controllers automatically based on the Swagger Doc you put together. If you look at the Swagger file in the editor (shown in step 3 above), the `x-swagger-router-controller` element (line 17 in the editor screenshot) specifies the name of the controller file associated with the `/hello` path. For example:
35
36```yaml
37 paths:
38 /hello:
39 x-swagger-router-controller: hello_world
40```
41
42The `operationId` is an optional element specifies which controller function to call. Without it, the router looks for a function of the same name as the HTTP Method for the operation (ie "get","post"). In this case (line 19), it is a function called `hello`. If the `operationId` were omitted, the function would be `get`. Learn [more](./docs/controllers.md).
43
44![screenshot of project editor](./docs/images/project-editor.png)
45
46Controller source code is always placed in `./api/controllers`. So, the controller source file for this project is `./api/controllers/hello_world.js`. To generate each of these controllers programmatically, run:
47
48```bash
49$ swagger project generate-routes
50```
51
52
53## 5. Write controller code in Node.js
54
55The generator gives you easy entry points for your API. All you have to do is fill in the blanks. Code your API's business logic in Node.js.
56
57excerpt from the generated `example_controller.js` controller
58```js
59// Operation get
60// Parameters expected:
61// name(Optional)
62controller["hello"] = function(req,res) {
63
64 //YOUR CODE GOES HERE
65 /*Example:
66 // variables defined in the Swagger document can be referenced using req.swagger.params.{parameter_name}
67 var name = req.swagger.params.name.value || 'stranger';
68 var hello = util.format('Hello, %s!', name);
69 // this sends back a JSON response which is a single string
70 res.json(hello);
71 */
72};
73```
74
75
76
77## 5. Test the server
78
79Run the project server. Confirm that all your endpoints function as you expect. Running the server with the following command will run it without any of the security protections provided through the middleman server, and will use snakeoil ssl certificates. The way it's set up by default, your api will actually restart automatically as you make changes to its source so you can see them in realtime. Here is where you debug and build your endpoints!
80
81```bash
82$ swagger project start
83```
84
85## 6. Add SSL
86
87Just drop your key and cert into the ssl folder of your server as `cert.cert` and `key.key`.
88
89## 7. Run it for realz
90
91The following command will run your server for real, with real ssl certs and HOTP request signing for the syndication server. The pm2 module is used to start it, and can be used to manage it as with any pm2 app. Now you're ready to offer your api and its secret key up to the syndication server, which will automatically take care of oauth, api keys, and rate limiting.
92
93```bash
94$ npm install -g pm2
95$ pm2 start app.js
96```
97
98
99# <a name="installation"></a>Installing the swagger module
100
101See the [Installing swagger](./docs/install.md) for details.
102
103# <a name="using"></a>Using the swagger module
104
105Go to the [swagger module doc page](./docs/README.md). It includes all the information you need to get started.
106
107# <a name="about"></a>About this project
108
109This initiative grew out of Apigee-127, an API design-first development framework using Swagger.
110Apigee donated the code to create the swagger-node project in 2015. __All modifications have been contributed by Jason "Toolbox" Oettinger of Duke's Innovation Co-Lab__
111
112 >Copyright 2015 Apigee Corporation
113
114 >Licensed under the Apache License, Version 2.0 (the "License");
115 you may not use this file except in compliance with the License.
116 You may obtain a copy of the License at
117
118 >http://www.apache.org/licenses/LICENSE-2.0
119
120 >Unless required by applicable law or agreed to in writing, software
121 distributed under the License is distributed on an "AS IS" BASIS,
122 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123 See the License for the specific language governing permissions and
124 limitations under the License.