获取 Express 路由器名称
Getting Express router name
如果我有以下 Express 路由器片段,我该如何编写一个中间件来获取函数名称?
var express = require('express'),
router = express.Router(),
....
router.get('/hello', helloWorld);
我需要做的是记录函数名helloWorld。鉴于我的 API 由 20 多个这样的函数组成,我只想使用 bunyan 或类似于审计的东西来记录这些函数。无论如何要在 Express 中执行此操作?
在 helloWorld
函数中你可以添加一行
var helloWorld = function(req, res, next){
var funName = helloWorld.name; // .name holds function name
}
或
function helloWorld(req, res, next){
var funName = helloWorld.name; // .name holds function name
}
更多信息请参考:function name
编辑:
如果我有以下 Express 路由器片段,我该如何编写一个中间件来获取函数名称?
var express = require('express'),
router = express.Router(),
....
router.get('/hello', helloWorld);
我需要做的是记录函数名helloWorld。鉴于我的 API 由 20 多个这样的函数组成,我只想使用 bunyan 或类似于审计的东西来记录这些函数。无论如何要在 Express 中执行此操作?
在 helloWorld
函数中你可以添加一行
var helloWorld = function(req, res, next){
var funName = helloWorld.name; // .name holds function name
}
或
function helloWorld(req, res, next){
var funName = helloWorld.name; // .name holds function name
}
更多信息请参考:function name