express-endpoints
easy way to extract and print list of express app rest api endpoints
Installation
npm install @igwtcode/express-endpoints
install express
npm install express
npm install -D @types/express # for typescript
Usage
javascript
import
module
package.json
{
"type": "module",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.18.2",
"@igwtcode/express-endpoints": "^1.0.0"
}
}
import express, { Router } from 'express';
import { ExpressEndpoints } from '@igwtcode/express-endpoints';
const app = express();
const router = Router();
commonjs
package.json
{
"type": "commonjs",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^4.18.2",
"@igwtcode/express-endpoints": "^1.0.0"
}
}
const express = require('express');
const { ExpressEndpoints } = require('@igwtcode/express-endpoints');
const app = express();
const router = express.Router();
example
router.get('/single', (req, res) => res.send(`${req.method} ${req.path}`));
router.post('', (req, res) => res.send(`${req.method} ${req.path}`));
router.put('/:id', (req, res) => res.send(`${req.method} ${req.path} ${req.params.id}`));
router.delete('/:id', (req, res) =>
res.send(`${req.method} ${req.path} ${req.params.id}`),
);
app.use('/tags', router);
app.get('/testGet', (req, res) => res.send(`${req.method} ${req.path}`));
app.post('/testPost', (req, res) => res.send(`${req.method} ${req.path}`));
app.use((err, req, res, next) => res.status(500).send('error'));
app.all('*', (req, res, next) => res.sendStatus(404));
const endpoints = new ExpressEndpoints(app);
console.log('*** with default options');
endpoints.print();
console.log('\n*** with color and custom prefix');
endpoints.print({ color: true, prefix: 'My-Api' });
console.log('\n*** colorized and short');
endpoints.print({ color: true, short: true });
console.log('\n*** items list', endpoints.items);
app.listen(3000, () => console.log('\nlistening on port 3000'));
typescript
import
module
package.json
{
"type": "module",
"scripts": {
"start": "ts-node-esm app.ts"
},
"dependencies": {
"express": "^4.18.2",
"@igwtcode/express-endpoints": "^1.0.0"
},
"devDependencies": {
"@types/express": "^4.17.16",
"@types/node": "^18.11.18",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "Node16",
"moduleResolution": "node16",
"esModuleInterop": true
}
}
commonjs
package.json
{
"type": "commonjs",
"scripts": {
"start": "ts-node app.ts"
},
"dependencies": {
"express": "^4.18.2",
"@igwtcode/express-endpoints": "^1.0.0"
},
"devDependencies": {
"@types/express": "^4.17.16",
"@types/node": "^18.11.18",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"moduleResolution": "node",
"esModuleInterop": true
}
}
import express, { Request, Response, Router, NextFunction } from 'express';
import { ExpressEndpoints } from '@igwtcode/express-endpoints';
const app = express();
const router = Router();
example
router.get('/single', (req: Request, res: Response) =>
res.send(`${req.method} ${req.path}`),
);
router.post('', (req: Request, res: Response) => res.send(`${req.method} ${req.path}`));
router.put('/:id', (req: Request, res: Response) =>
res.send(`${req.method} ${req.path} ${req.params.id}`),
);
router.delete('/:id', (req: Request, res: Response) =>
res.send(`${req.method} ${req.path} ${req.params.id}`),
);
app.use('/tags', router);
app.get('/testGet', (req: Request, res: Response) =>
res.send(`${req.method} ${req.path}`),
);
app.post('/testPost', (req: Request, res: Response) =>
res.send(`${req.method} ${req.path}`),
);
app.use((err: any, req: Request, res: Response, next: NextFunction) =>
res.status(500).send('error'),
);
app.all('*', (req: Request, res: Response, next: NextFunction) => res.sendStatus(404));
const endpoints = new ExpressEndpoints(app);
console.log('*** with default options');
endpoints.print();
console.log('\n*** with color and custom prefix');
endpoints.print({ color: true, prefix: 'My-Api' });
console.log('\n*** colorized and short');
endpoints.print({ color: true, short: true });
console.log('\n*** items list', endpoints.items);
app.listen(3000, () => console.log('\nlistening on port 3000'));
Sample Output
License
MIT (see license)