JSDocs: Documenting Node.js express routes

16,420

Solution 1

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 Updated Screenshot - Module: routers/users Namespace - usersRouter

Solution 2

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" });
});
Share:
16,420
Soatl
Author by

Soatl

I am a polyglot developer with a Masters in Computer Science from Georgia Tech. I focus on full-stack development and enjoy learning about cybersecurity principles and machine learning. I also have experience with big data as well as DevOps. #SOreadytohelp

Updated on June 13, 2022

Comments

  • Soatl
    Soatl about 2 years

    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?

  • ksugiarto
    ksugiarto almost 9 years
    Hi, I try using this solution but it is resulting nothing, hmm. Can you kindly post the url where you find this?
  • leo60228
    leo60228 almost 9 years
  • damd
    damd about 7 years
    The screenshot URL returns 404 now :(
  • Pax Beach
    Pax Beach over 5 years
    20 hours spent my time looking for resolving the issue. Thank you so much, it works good.