scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 228 forks source link

No operations defined in spec! #365

Open devmbilal opened 10 months ago

devmbilal commented 10 months ago

App.JS

app.js require('dotenv').config();

const express = require('express'); const connectDB = require('./server/config/db'); const swaggerJsDoc = require('swagger-jsdoc'); const swaggerUi = require('swagger-ui-express');

const app = express(); const port = process.env.PORT || 5000; connectDB();

const swaggerOptions = { swaggerDefinition: { info: { title: 'SJP App API', version: '1.0.0', description: 'Smart Journey Planner App API Information', }, servers: ['http://localhost:5000'], }, apis: ['./server/routes/routes.js'], };

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

ROUTES.JS const express = require('express'); const router = express.Router();

const stopController = require('../controllers/stops/stopController');

// Stops router.post('/addstop', stopController.postStop);

module.exports = router;

const Stop = require('../../models/stop/Stop'); const mongoose = require('mongoose');

/**

exports.postStop = async (req, res) => { try { const highestStop = await Stop.findOne().sort({ stopId: -1 });

    let nextStopId;
    if (highestStop) {
        const match = highestStop.stopId.match(/\d+/);
        const highestStopId = match ? parseInt(match[0]) : 0;
        nextStopId = `STP-${highestStopId + 1}`;
    } else {
        nextStopId = "STP-1";
    }

    const newStop = new Stop({
        stopId: nextStopId,
        stopName: req.body.stopName,
        latitude: req.body.latitude,
        longitude: req.body.longitude
    });

    await Stop.create(newStop);
    res.status(200).json({ message: 'Stop added successfully', stop: newStop });
} catch (err) {
    console.error(err);
    res.status(500).json({ error: 'Error adding stop' });
}

}