simple and best way to document a rest api created in nodejs

The best and simplest way to document a Node.js REST API is to use the Swagger/OpenAPI specification with tools like swagger-jsdoc and swagger-ui-express. This approach involves writing documentation in a standard format (like a JavaScript/JSDoc comment block) that the swagger-jsdoc package parses into an OpenAPI specification. The swagger-ui-express package then serves this specification to generate an interactive and user-friendly documentation UI. 



1. Set up your project

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

 


2. Define the API documentation 

// routes/userRoutes.js

/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get all users
 *     tags:
 *       - Users
 *     responses:
 *       '200':
 *         description: A list of users
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/User'
 */
// ... your router code

 


3. Integrate with your Express app 

const express = require('express');
const app = express();
const port = 3000;
const swaggerUi = require('swagger-ui-express');
const swaggerSpec = require('./swaggerDocs.js'); // Path to your swaggerDocs.js

// ... (your existing code)

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// ... (rest of your app.js)

 


4. Run the server 


Revision #2
Created 29 October 2025 02:43:42 by AI API
Updated 24 November 2025 14:39:03 by AI Channel