Created
July 12, 2014 00:18
-
-
Save hiroosak/cf0480120cb86af5bddc to your computer and use it in GitHub Desktop.
swagger-expressを使って簡単にAPIドキュメントを作成する ref: http://qiita.com/taizo/items/320d964e1cb81131de67
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @swagger | |
* resourcePath: /api | |
* description: All about API | |
*/ | |
/** | |
* @swagger | |
* path: /login | |
* operations: | |
* - httpMethod: POST | |
* summary: Login with username and password | |
* notes: Returns a user based on username | |
* responseClass: User | |
* nickname: login | |
* consumes: | |
* - text/html | |
* parameters: | |
* - name: username | |
* description: Your username | |
* paramType: query | |
* required: true | |
* dataType: string | |
* - name: password | |
* description: Your password | |
* paramType: query | |
* required: true | |
* dataType: string | |
*/ | |
exports.login = function (req, res) { | |
var user = {}; | |
user.username = req.param('username'); | |
user.password = req.param('password'); | |
res.json(user); | |
} | |
/** | |
* @swagger | |
* models: | |
* User: | |
* id: User | |
* properties: | |
* username: | |
* type: String | |
* password: | |
* type: String | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var swagger = require('swagger-express'); | |
app.use(swagger.init(app, { | |
apiVersion: '1.0', | |
swaggerVersion: '1.0', | |
swaggerURL: '/docs', // swaggerページのパス | |
swaggerJSON: '/api-docs', // swagger表示用のデータアクセス先 | |
swaggerUI: './dist', // swagger-uiが置いてあるパス | |
basePath: 'http://localhost:3000', | |
apis: ['./api.js'], // ドキュメントが記載されているファイル | |
middleware: function(req, res){} | |
})); | |
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir sampleApp | |
$ npm init | |
$ npm install express --save | |
$ npm install swagger-express --save-dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd .. | |
$ git clone git@github.com:wordnik/swagger-ui.git | |
$ cp -R swagger-ui/dist sampleApp/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
window.swaggerUi = new SwaggerUi({ | |
url: "http://localhost:3000/api-docs", // ここを書き換える | |
dom_id: "swagger-ui-container", | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment