mkdri app01 cd app01 npm init -y
Függőségek:
pnpm install --save express nodemon morgan pnpm install --save-dev swagger-jsdoc swagger-ui-express
Script:
{ "scripts": { "start":"nodemon app --watch app" } }
const express = require('express') const app = new express() const morgan = require('morgan') const router = require('./routes/api') const PORT = process.env.PORT || 8000; app.use(express.json()) app.use(morgan('tiny')) app.use(router) app.listen(PORT, () => { console.log('Server is running on port: ', PORT) })
const express = require('express') const app = new express() const morgan = require('morgan') const router = require('./routes/api') const swaggerUi = require('swagger-ui-express') const swaggerDocument = require('../swagger.json') const PORT = process.env.PORT || 8000; app.use('/swagger', swaggerUi.serve) app.get('/swagger', swaggerUi.setup(swaggerDocument)) app.use(express.json()) app.use(morgan('tiny')) app.use(router) app.listen(PORT, () => { console.log('Server is running on port: ', PORT) })
const router = require('express').Router(); router.get('/ping', async (req, res) => { res.send({message: 'Helló'}) }) module.exports = router
{ "swagger": "2.0", "paths": { "/ping": { "get": { "operationId": "pingEndpoint", "description": "Egyszerű elérés ellenőrzés", "responses": { "200": { "description": "Siker" } } } } } }
Nézzük meg az eredményt:
{ "swagger": "2.0", "basePath": "/api", "paths": { "/employees": { "get": { "description": "Dolgozók lekérése", "responses": { "200": { "description": "Siker" } } } } } }