javascript - what does it mean by "debug = require('debug')('api:server')" - S

I was reading some code of a project to learn node.js then I found this line (debug = require('deb

I was reading some code of a project to learn node.js then I found this line (debug = require('debug')('api:server')) which is enclosed in brackets. As I'm new to programming and when I don't know something I just search it on the web, but I couldn't find an answer for this one. If you are going to tell me to search on the web more aggressively then please tell me HOW too.

I was reading some code of a project to learn node.js then I found this line (debug = require('debug')('api:server')) which is enclosed in brackets. As I'm new to programming and when I don't know something I just search it on the web, but I couldn't find an answer for this one. If you are going to tell me to search on the web more aggressively then please tell me HOW too.

Share Improve this question asked Jan 23, 2020 at 5:53 Sukhchain SinghSukhchain Singh 831 silver badge6 bronze badges 1
  • I would remend that you read the node.js docs: nodejs/en/docs – Chris Herbst Commented Jan 23, 2020 at 6:07
Add a ment  | 

1 Answer 1

Reset to default 5

require returns the exports of some other module. Here, since debug is being passed into require, the debug module is being required. What this module does is:

debug exposes a function; simply pass this function the name of your module, and it will return a decorated version of console.error for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.

So

const debug = require('debug')('api:server');

where require('debug') resolves to a function, is like:

const debug = deccorateModule('api:server');

where decorateModule carries out the functionality described above. In this case, require acts as a higher-order function: a function which returns a function. (You may likely have a module named api:server)

This results in the debug variable holding the decorated version of console.error.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583012a4634363.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信