day0ops
day0ops

Reputation: 7482

Getting Express router name

If I have the following Express router snippet how can I write a middleware to get the function name ?

var express = require('express'),
router = express.Router(),
....
router.get('/hello', helloWorld);

What I need to do is log the function name helloWorld. Given that my API is made up of 20+ such functions I just want to log these using bunyan or something similar to audit. Anyway to do this in Express ?

Upvotes: 0

Views: 733

Answers (1)

Avinash Vadlamudi
Avinash Vadlamudi

Reputation: 301

Inside the helloWorld function you can add a line as

var helloWorld = function(req, res, next){
  var funName =  helloWorld.name; // .name holds function name
}

or

function helloWorld(req, res, next){
  var funName =  helloWorld.name; // .name holds function name
}

For more info refer: function name

Edit:

Please refer: middleware.router

Upvotes: 1

Related Questions