what does it mean by “debug = require('debug')('api:server')”

那年仲夏 提交于 2021-02-05 09:25:30

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/59872296/what-does-it-mean-by-debug-requiredebugapiserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!