Created
March 4, 2019 14:50
-
-
Save miyachin/5a018393da972ea0e2ff2b14222a6907 to your computer and use it in GitHub Desktop.
Swagger UIから参照するSwagger Specが書かれたindex.js
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 express = require('express'); | |
var app = express(); | |
var swaggerJSDoc = require('swagger-jsdoc'); | |
app.use(function(req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
var options = { | |
swaggerDefinition: { | |
info: { | |
title: 'Hello World', | |
version: '1.0.0.' | |
}, | |
}, | |
apis: ['./index.js'], | |
}; | |
var swaggerSpec = swaggerJSDoc(options); | |
app.get('/api-docs.json', function(req, res){ | |
res.setHeader('Content-Type','application/json'); | |
res.send(swaggerSpec); | |
}); | |
/** | |
* @swagger | |
* /user/{user_id}: | |
* get: | |
* description: Get specific user data | |
* produces: | |
* - application/json | |
* parameters: | |
* - name: user_id | |
* description: UserID to use for login. | |
* in: path | |
* required: true | |
* type: integer | |
* responses: | |
* 200: | |
* description: login | |
* examples: | |
* user: | |
* user_id: 334857 | |
* user_name: Naruki Miyachi | |
*/ | |
app.get('/user/:userId', function(req, res) { | |
// 実際は、ここにparamのuserIdを使って、SQLを実行したりする処理が入る。 | |
res.json({"user_id": 334857, "user_name": "Naruki Miyachi"}); | |
}); | |
//listen | |
app.listen(3000, function(){ | |
console.log("Listen on port 3000."); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment