Skip to content

Instantly share code, notes, and snippets.

@nevill
Created December 11, 2013 05:54
Show Gist options
  • Save nevill/7905669 to your computer and use it in GitHub Desktop.
Save nevill/7905669 to your computer and use it in GitHub Desktop.
A sample to use swagger-jack
module.exports = {
resourcePath: '/orgs',
apis: [{
path: '/orgs',
operations: [{
httpMethod: 'POST',
responseClass: 'void',
nickname: 'returnParams',
parameters: [{
dataType: 'Org',
required: true,
paramType: 'body'
}]
}]
}],
models: {
Org: {
id: "Org",
properties: {
id: {
type: 'string',
required: true,
description: 'ID of the Org'
},
name: {
type: 'string',
description: 'Name of the Org'
}
}
}
}
};
var http = require('http');
var express = require('express');
var swagger = require('swagger-jack');
var app = express();
var root = 'http://localhost:3000';
app.use(express.bodyParser())
.use(express.methodOverride())
.use(swagger.generator(app, {
apiVersion: '1.0',
basePath: root + "/api",
},
[
{
api: require('./creation.js'),
controller: {
returnParams: function(req, res) {
res.json(req.body);
}
}
}
]))
.use(swagger.validator(app))
.use(swagger.errorHandler());
http.createServer(app).listen(3000, function() {
console.log('Open your browser to visit', root);
});
Success output
> $ http -v -f localhost:3000/api/orgs id="whatever" name="TEsting org"
POST /api/orgs HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, compress
Content-Length: 28
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:3000
User-Agent: HTTPie/0.7.2
id=whatever&name=TEsting+org
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 47
Content-Type: application/json; charset=utf-8
Date: Wed, 11 Dec 2013 05:49:20 GMT
X-Powered-By: Express
{
"id": "whatever",
"name": "TEsting org"
}
=============================================================================================================================
Failed output
> $ http -v -f localhost:3000/api/orgs name="TEsting org"
POST /api/orgs HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, compress
Content-Length: 16
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: localhost:3000
User-Agent: HTTPie/0.7.2
name=TEsting+org
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 49
Content-Type: application/json; charset=utf-8
Date: Wed, 11 Dec 2013 05:49:17 GMT
X-Powered-By: Express
{
"message": "body property 'id' is required"
}
{
"name": "swagger-test",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "BSD-2-Clause",
"dependencies": {
"swagger-jack": "~1.6.2",
"express": "~3.4.6"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment