node.js如何configurationlogging器的工作?
我正在阅读Nike Cantelon的一本名为“Node.js in Action”的书,并坚持使用可configuration的logging器实现:
当使用String.prototype.replace()时,我们设置了一个函数(match,property)作为第二个参数,我对它的function毫无头绪 。 任何人都可以解释什么function(比赛,财产)呢? 写代码的方式并没有给我一个洞察力。
function setup(format){ let regex = /:(\w+)/g; return function logger(req, res, next){ let str = format.replace(regex, (match, property) => { return req[property]; }); console.log(str); next(); } } module.exports = setup;
String.replace()函数有两个参数: searchvalue
和newvalue
。 这意味着您正在searchformat
string中的特定模式。 find时,用req[property]
的值代替。 所以这个函数得到这个property
作为它的参数,然后把它作为req
对象的一个键,把值返回,并用format
stringreplacefind的外观。