简体   繁体   中英

how to use Form in swagger / Node.JS

I am new to using Swagger. So I would be needing help of some sort. I am trying to use swagger, like incoporate into my Rest api written in Node.JS The REST api works very perfectly when tested on Postman but i added the swagger part and the form on Swagger does not work as its supposed to. The form data part which is supposed to work is not working fine.

My full code is looking like this

var express = require('express');
var app = express();
const cors = require("cors");
var bodyParser = require('body-parser');
var mysql = require('mysql');
const swaggerJSDoc = require('swagger-jsdoc');  
const swaggerUI = require('swagger-ui-express'); 
var port = process.env.PORT || 7000;

const swaggerOptions = {  
    swaggerDefinition: {  
        info: {  
            title:'*****REST API',  
            version:'1.0.0'  
        }  
    },  
    apis:['***.js'],  
}

const swaggerDocs = swaggerJSDoc(swaggerOptions);  
app.use('/api-docs',swaggerUI.serve,swaggerUI.setup(swaggerDocs));  

//evade cors

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));


app.get('/api/', function (req, res) {
    return res.send({ error: false, message: 'hello from api' })
});


var dbConn = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: '',
    database: '*****'
});

module.exports = dbConn;

//functions

/** 
 * @swagger 
 * /auth/register: 
 *   post: 
 *     description: Create new user 
 *     parameters: 
 *     - name: fullname 
 *     - name: user 
 *     - name: pass
 *     
 *       description: Create an new employee 
 *       in: formData 
 *       type: String 
 *     responses:  
 *       200: 
 *         description: Created  
 *   
 */

app.post('/auth/register', function (req, res) {
    var fullname = req.body.fullname;
    var user = req.body.user;
    var pass = req.body.pass;

    dbConn.query('INSERT INTO users (fullname, user, pass) VALUES (?,?,?)', [fullname, user, pass], function (error, results, fields) {
        if (error) throw error;
        return res.send({ error: false, data: results, message: 'Account Setup Complete' });
    })
})

/** 
 * @swagger 
 * /auth/login: 
 *   post: 
 *     description: Login User 
 *     parameters: 
 *     - name: user 
 *     - name: pass
 *     
 *       description: Login User 
 *       in: formData 
 *       type: String 
 *     responses:  
 *       200: 
 *         description: Login  
 *   
 */ 

app.post('/auth/login',function(req,res){
    var user = req.body.user;
    var pass = req.body.pass;

    dbConn.query('SELECT * FROM users WHERE user = ? AND pass =?', [user,pass], function (error, results, fields){
        if(results.length > 0){
            return res.send({error:false, message: 'OK'});
        }else{ 
            return res.send({error: true, message: 'Incrorrect Login Details'});
        }
    });
})



app.listen(port, function () {
    console.log('App running on Port: ' + port);
});

module.exports = app;

What could I be doing wrong? Is there something I need to put correctly?

I managed to fix it, setting the parameters like this got to work for me.

/** 
 * @swagger 
 * /auth/register: 
 *   post: 
 *     description: Create new user 
 *     parameters: 
 *     - name: fullname 
 *       description: fullname of user 
 *       in: formData 
 *       type: String 
 *     - name: user 
 *       description: userid of user 
 *       in: formData 
 *       type: String 
 *     - name: pass 
 *       description: pass of user 
 *       in: formData 
 *       type: String 
 *     responses:  
 *       200: 
 *         description: Created  
 *   
 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM