Skip to content

Instantly share code, notes, and snippets.

@miyachin miyachin/index.js
Created Mar 4, 2019

Embed
What would you like to do?
Swagger UIから参照するSwagger Specが書かれたindex.js
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
You can’t perform that action at this time.