Extends express routes adding descriptor function. In descriptor you can add the object representing the parameters that you need.
app.get('/books', function(req, res) {
res.send("My fantastic book");
}).descriptor({
name :'Retrieve a fantastic book',
params : []
});
Basic example
var express = require('express');
var api = require('express-list-endpoints-descriptor')(express);
var app = express();
app.get('/user', function(req, res) {
res.json({user:true})
}).descriptor({
name :'Get list of users'
});
app.get('/api', function(req, res) {
res.send(api.listAllEndpoints(app));
}).descriptor({
name :'My first API documentation'
});
app.listen(8080, function(){
console.log('Example app listening on port 8080!');
});
Browsing http://localhost:8080/api will return
[
{
"path": "/user",
"methods": ["GET"],
"descriptor": [{"name": "Get list of users"}]
},
{
"path": "/api",
"methods": ["GET"],
"descriptor": [{"name": "My first API documentation"}]
}
]
Inside descriptor object you will find the object that you defined inside the descriptor function after route definition.
Keys definition
path: [STRING]
methods : [ARRAY of strings]
Methods for the same endpoint e.g. ['GET','POST']
descriptor: [ARRAY of objects]
Object user defined inside descriptor function (in the same order of methods array)
[{name:'description of /users with method get'}, {name:'description of /users with method post']
Mehods
api.listAllEndpoints(server)
retrieve all the mapped Express APIs even if the ones that were not extended by the descriptor function.
The object server is an instance of express.
api.listEndpoints(server) :
retrieve only the mapped Express routes, that extend with the descriptor function. The object server is an instance of express.
Retrieve all endpoints list
app.get('/apis', function(req, res) {
res.send(api.listAllEndpoints(app));
}).descriptor({
name :'Retrieve APIs documentation'
});
Retrieve endpoints list that has been extended
app.get('/apis', function(req, res) {
res.send(api.listEndpoints(app));
}).descriptor({
routedescr :'Retrieve APIs list only from routes that have it'
});
Usage
The express-list-endpoints-descriptor module allows to create documentation for Express APIs.
Works with Your router instance (router) or your app instance (app).
Example - APP instance
var express = require('express');
var api = require('express-list-endpoints-descriptor')(express);
var app = express();
app.get('/user/:ID', function(req, res) {
res.json({user:true})
}).descriptor({
name :'Get details about user',
params: {ID:'STRING'}
});
//will not appear on endpoints = api.listEndpoints(app);
app.post('/user/:ID', function(req, res) {
res.json({user:true})
})
app.get('/api', function(req, res) {
res.send(api.listAllEndpoints(app));
}).descriptor({
name :'My first API documentation'
});
app.listen(8080, function(){
console.log('Example app listening on port 8080!');