简体   繁体   中英

JSDocs: Documenting Node.js express routes

I am struggling documenting router.get calls with JSDocs. I am unable to get the documentation to display correctly on the page if I try to append it to my router call itself.

/**
 * Health check
 * @memberof health
 */
router.get('/happy', function(req, res) {
    res.json({ "status" : "OK" });
});

To resolve it, I made the functions have names.

router.get('/happy', happy);

/**
 * Health check
 * @memberof health
 */
function happy(req, res) {
    res.json({ "status" : "OK" });
}

This works, but I would really like to find a way to get the first method to work. Is there a way to document the first example? A keyword I can use?

I do the following in my code.

/** Express router providing user related routes
 * @module routers/users
 * @requires express
 */

/**
 * express module
 * @const
 */
const express = require('express');

/**
 * Express router to mount user related functions on.
 * @type {object}
 * @const
 * @namespace usersRouter
 */
const router = express.Router();

/**
 * Route serving login form.
 * @name get/login
 * @function
 * @memberof module:routers/users~usersRouter
 * @inner
 * @param {string} path - Express path
 * @param {callback} middleware - Express middleware.
 */
router.get('/login', function(req, res, next) {
  res.render('login', {title: 'Login', message: 'You must login'});
});

And the output is: Screenshot 更新的屏幕截图 - 模块:路由器/用户 命名空间 -  usersRouter

From a little bit of Googling, haven't actually tested.

/**
 * Health check
 * @memberof health
 * @function
 * @name happy
 */
router.get('/happy', function(req, res) {
    res.json({ "status" : "OK" });
});

如果你没有在文件的顶部放置“@module moduleName ”,jsdoc将不会引用页面上的其他注释,因为它们没有父文件@module

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM