我有一个名为client
的变量,它在类的函数中定义。我只想将其导出到该类之外,以便可以在控制器中使用它。
这是我的代码
const path = require('path');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const flash = require('connect-flash');
var methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const http = require('http');
const socketio = require('socket.io');
const passport = require('passport');
const rememberLogin = require('./http/middlewares/rememberLogin');
const Helpers = require('./helpers');
const app = express();
module.exports = class Application{
constructor() {
this.setExpress();
this.setupMongo();
this.setConfig();
this.setRoutes();
// this.updateDataBase();
}
setExpress() {
const server = http.createServer(app);
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
let client = socketio(server);
}
setupMongo() {
mongoose.connect(config.database.url, {
useUnifiedTopology: true,
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false
}).then(() => console.log('Connected to mongo....'));
}
setConfig() {
require('./passport/passport-local');
// Set ejs config
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '../resource/views'));
app.use(config.layout.ejs.expressLayouts);
app.set("layout extractScripts", config.layout.ejs.extractScripts);
app.set("layout extractStyles", config.layout.ejs.extractStyles);
app.set("layout" , config.layout.ejs.master);
app.use(express.static(path.join(__dirname, '../public')));
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use(bodyParser.urlencoded({ extended : true }));
app.use(session({...config.session}));
app.use(cookieParser('Thisismycookiesecretekey'));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(rememberLogin.handle);
app.use((req, res, next) => {
app.locals = new Helpers(req, res).getObjects();
next();
})
}
setRoutes() {
app.use(require('./routes/index'));
}
}
module.exports.client = client;
我只是通过从类中删除setupExpress函数来弄清楚的,然后我可以访问
client
变量。
但是 我认为这不是正确的方法。
有什么办法可以以不同的方式将其导出?!