Skip to content

Commit

Permalink
Working login example
Browse files Browse the repository at this point in the history
  • Loading branch information
chdanielmueller committed Jun 6, 2015
1 parent a6b81b6 commit 5313d40
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
5 changes: 5 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

// Dependencies
var express = require('express'),
bodyParser = require('body-parser'),
routes = require('./routes'),
swagger = require('../');

// Initialize express
var app = express();
app.use(bodyParser.json()); // To support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // To support URL-encoded bodies
extended: true
}));

// Swagger definition (https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#schema)
var swaggerDefinition = {
Expand Down
48 changes: 35 additions & 13 deletions example/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,53 @@ module.exports.setup = function (app) {
/**
* @swagger
* /:
* get:
* responses:
* 200:
* description: hello world
* get:
* description: Returns Hello World!
* responses:
* 200:
* description: hello world
*/
app.get('/', rootHandler);

/**
* @swagger
* /login:
* post:
* responses:
* 200:
* description: login
* post:
* description: Login to the application
* produces:
* - application/json
* parameters:
* - name: username
* description: Username to use for login.
* in: formData
* required: true
* type: string
* - name: password
* description: User's password.
* in: formData
* required: true
* type: string
* responses:
* 200:
* description: login
*/
app.get('/login', loginHandler);
app.post('/login', loginHandler);
};

/**
* Handler for the Homepage
* @param {object} req - Express request
* @param {object} res - Express response
*/
function rootHandler(req, res) {
res.send('Hello World!');
}

/**
* Handler for the Login
* @param {object} req - Express request
* @param {object} res - Express response
*/
function loginHandler(req, res) {
var user = {};
user.username = req.param('username');
user.password = req.param('password');
res.json(user);
res.json(req.body);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"swagger-tools": "^0.8.7"
},
"devDependencies": {
"body-parser": "^1.12.4",
"eslint": "^0.22.1",
"express": "^4.12.4"
}
Expand Down

0 comments on commit 5313d40

Please sign in to comment.