1
我有兩個文件:如何訪問server.js文件之外的應用程序?
server.js
const app = express();
require('./server/config/crawlerBasedModels.config').basedModels()
.then((models) => {
app.locals.basedModels = models;
console.log(app.locals.basedModels);
})
.catch(err => console.log(err));
crawlerBasedModels.config:
const fs = require('fs');
const Promise = require('bluebird');
module.exports = {
basedModels:() => {
return new Promise((res, rej) => {
let basedModelsPath = require('../config/exportFolderPath');
const basedModels = JSON.parse(fs.readFileSync(basedModelsPath + '/basedModels.json'));
if(basedModels) {console.log(basedModels); return res(basedModels);}
return rej('couldnt load basedModels file!');
});
}
};
控制檯,我看到: 未定義
我的問題是,如何在我的應用程序中設置一個全局變量,以便在整個應用程序生命週期中公開?