我正在使用nodejs + Express开发REST API,并一直在README文件中记录我的API,我想知道是否有可能自动化它。例如,给定:
app.get('/path/to', dosomething);
app.post('/path/to/:somethingelse', scream);
我希望它自动生成这个
GET: /path/to dosomething
POST: /path/to/:somethingelse scream
我正在使用nodejs + Express开发REST API,并一直在README文件中记录我的API,我想知道是否有可能自动化它。例如,给定:
app.get('/path/to', dosomething);
app.post('/path/to/:somethingelse', scream);
我希望它自动生成这个
GET: /path/to dosomething
POST: /path/to/:somethingelse scream
你可以接近实现目标。
查看'res'对象。你会发现它引用了你的应用对象。 因此,res.app._router.map包含一组数组,用于http方法(get、post等)。 假设在GET数组中,有一个路径和一个回调属性。路径将给出路由url,回调是路由处理程序的数组。从这里,你可以获取函数名称。
所以...
创建一个新路由,仅用于将你的文档输出到文件中。在该路由处理程序中,解析res.app._router.map.GET、res.app._router.map.POST等。
不是最理想的方法,但可行。
这是Javascript,您可以轻松地修补原始方法以生成文档。
以下是Coffeescript中的示例代码:
express = require 'express'
methods = require 'express/node_modules/methods' # array of all HTTP methods
app = express()
methods.forEach (method) ->
orig = app[method]
app[method] = (path, handler) ->
console.log "patched method ", method, " ", path
# generate docs here
orig.apply(this, arguments)
您还可以使用handler.toString()
来获取处理程序函数的代码。再加上一些正则表达式技巧,您可以从这样编写的函数中提取更多的注释:
app.get "/foo", (req, res) ->
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit"
more code here
我认为最好的方法是找到或开发一个 JSDoc
插件,以解析自定义文档块并添加新标签,与原生的 jsdoc 标签结合使用,例如下面的示例:
NB:以下示例不完整,没有必要重复说明...
'use strict';
/**
* @file defines all server routes for the Article resource
* @name articles.server.routes.js
* @author Rémi Becheras <rbecheras@sirap.fr>
*/
/**
* @namespace articles.server.routes
*/
/**
* @module articles/server/routes
*/
/**
* Article policy object
* @var {Policy}
* @inner
*/
var articlesPolicy = __.require('articles.server.policy');
/**
* Article controller
* @var {Controller}
* @inner
*/
var articles = __.require('articles.server.controller');
// NB: `__.require()` is a project-specific method working as an helper for the native `require()` method, `__` is an object binded to the global context
/**
* Exports a function that defines all routes of the `articles` module, binding it to the express `app` instance.
* @function
* @param {object} app The express application instance
* @return void
*/
module.exports = function (app) {
/**
* Articles REST API resource end-point
* @endpoint /api/articles
* @name apiArticles
* @version v1
* @since v1
* @description Articles REST API resource end-point
*/
app.route('/api/articles').all(articlesPolicy.isAllowed)
.get(articles.list)
/**
* Create a new article
* @route
* @verb POST
* @name postArticle
* @description If the user is logged in and has the 'author' role, it can create an article w
* @example
POST http://example.com/api/articles \
--data { title: "my title", body: "<h1>my content</h1>" }
*/
.post(articles.create);
// Single article routes
app.route('/api/articles/:articleId').all(articlesPolicy.isAllowed)
.get(articles.read)
.put(articles.update)
.delete(articles.delete);
// Finish by binding the article middleware
app.param('articleId', articles.articleByID);
};
.toString()
时,它们会显示出来。请注意缩小器或咖啡脚本编译器。 - Diogo Gomes