2018.09.12 09:55

Node.js - 如何从目录递归加载模块

快速如何将node.js中的文件作为模块加载。

index.js:

require('fs').readdirSync(__dirname).forEach(function (file) {
  /* If its the current file ignore it */
  if (file === 'index.js') return;

  /* Store module with its name (from filename) */
  module.exports[path.basename(file, '.js')] = require(path.join(__dirname, file));
});

ping.js(示例模块):

module.exports = (function (params, request) {
  broadcastUser({data: 'pong'}, 'message', request);
});

就这样!

DIY表情

(添加http或https协议)

提交评论