2017-03-24 6 views
0

は私のmodelsフォルダで私は..FSからモジュールをエクスポートできますか?

// Account.js 
module.exports = mongoose.model('Account', AccountSchema) 
... 
// Rules.js 
module.exports = mongoose.model('Rules', RulesSchema) 

そして、私のindex.jsファイル(同じフォルダ./models)のようにたくさんのファイルを持っています。輸出

// index.js 
const mathJSFiles = /^[^index].*\.js$/gmi; 
fs.readdirSync('.') 
    .filter(file => file.search(mathJSFiles) >= 0) 
    .forEach(file => { 
    file = path.basename(file, '.js') 
    exports[file] = require(join(models, file)) 
    }) 

という名前のよう 理由は、だから、別のファイルmain.jsで、私はそのようなをしたい./modelsフォルダ内のすべてのファイルを検索することであり、輸出...

import * as Models from './models' 
Models.Account 

または

import { Account } from './models' 

これは可能ですか?

+0

'* as'は何ですか?あなたはCommonjsモジュールとES6を混ぜるだけですか?実際には、普通の 'const models = require( './ models')' – Bergi

+0

よろしくお願いします。私は私の答えを変える。 – ridermansb

答えて

0

私は、次のような何かを行うことをお勧めします:

// main.js 
var glob = require('glob') 
    , path = require('path'); 

glob.sync('./models/**/*.js').forEach(function(file) { 
    require(path.resolve(file)); 
}); 
関連する問題