require('module1') // 没有定义命名,如何使用?
var module2 = require('module2') // 可以使用module2调用模块里的函数
批量导入模块
var models_path = __dirname + '/app/models'
var walk = function(path) {
fs
.readdirSync(path)
.forEach(function(file) {
var newPath = path + '/' + file
var stat = fs.statSync(newPath)
if (stat.isFile()) {
if (/(.*)\.(js|coffee)/.test(file)) {
require(newPath)
}
}
else if (stat.isDirectory()) {
walk(newPath)
}
})
}
walk(models_path)
这里导入的模块如何调用模块里的方法呢?如果这样无法使用模块那么这样可以批量导入并可以使用模块?
模块里用全局变量吧。但是不推荐,维护麻烦,你还是指定名字吧,用一个变量来存放