Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在express服务器的成员变量(类实例)之间传递数据的体系结构/方法_Javascript_Typescript_Express - Fatal编程技术网

Javascript 在express服务器的成员变量(类实例)之间传递数据的体系结构/方法

Javascript 在express服务器的成员变量(类实例)之间传递数据的体系结构/方法,javascript,typescript,express,Javascript,Typescript,Express,我正在为express编写一个类,用作库的监控服务器。我需要创建不同的路由器和路由,但我需要从monitor server类访问函数和变量。我刚刚将这个变量传递给了不同的路由。我是否应该使用更好的解决方案/架构来构建不同的路由器并初始化它们?express服务器需要表示为类 monitor-server.ts auth.ts 如果MonitorServer中的方法和变量是公共的,您应该能够使用“monitor”变量调用它们 例如: monitor-server.ts auth.ts 这回答了你的

我正在为express编写一个类,用作库的监控服务器。我需要创建不同的路由器和路由,但我需要从monitor server类访问函数和变量。我刚刚将这个变量传递给了不同的路由。我是否应该使用更好的解决方案/架构来构建不同的路由器并初始化它们?express服务器需要表示为类

monitor-server.ts auth.ts
如果MonitorServer中的方法和变量是公共的,您应该能够使用“monitor”变量调用它们

例如:

monitor-server.ts auth.ts
这回答了你的问题吗?或者您是否想知道,如果没有auth.ts中的setMonitorServer()调用,如何执行此操作?

正如您在示例中所示,我当前使用的是this关键字,但传递this关键字是正确的方法还是有其他方法?而不将其传递给authRouter。也许可以用另一种方式使用authRouter而不仅仅是导出它?如果您真的想将它们解耦,而不是让类的定义需要authRouter,您可以公开MonitorServer的实例化并让authRouter使用它。如果没有更多的上下文,我不确定您是如何使用它们的,但我假设只有一个MonitorServer实例和一个AuthRouter实例。因此您可以执行其他操作。ts
const monitorServer=new monitorServer();导出监控服务器auth.ts
从“某处”导入monitorServer;if(monitorServer){monitorServer.somePublicFunction()}
import * as express from 'express'
import * as path from 'path';
import * as cookieParser from 'cookie-parser';
import * as logger from 'morgan';
import * as cors from 'cors';
import * as useragent from 'express-useragent';
import * as http from 'http';
import * as process from 'process';
import {EventEmitter} from "events";
const authRouter = require('./routes/auth');
export class MonitorServer extends EventEmitter {
    public app: express.Application;
    private server: http.Server ;
    private port = 3000;
    private type: 0 | 1 = 1;
    private dBMode: 0 | 1 | 2 = 0;
    private admin ?: {
        username: string;
        password: string;
    };
    constructor() {
        super();
        this.app = express();
        this.app.set('port', this.port);
        this.app.set('type', this.type);
        this.initializeMiddlewares();
        this.initializeControllers();
        this.listen();
    }
    private initializeMiddlewares() {
        // initialize middlewares
    }
    private initializeControllers() {
        this.app.use('/auth', authRouter.router);
        authRouter.setMonitorServer(this);
    }
    public listen() {
        this.server = http.createServer(this.app);
        this.server.listen(this.port);
    }
}
import * as express from "express";
import {MonitorServer} from "../monitor-server";
let router = express.Router();
let monitor: MonitorServer;
function setMonitorServer(monitorServer: MonitorServer) {
    monitor = monitorServer;
}
router.get('/admin', (req, res) => {
    // access variables and data within monitor-server class
});
router.get('/operationMode', (req, res) => {
    // access variables and data within monitor-server class
});
module.exports = {router, setMonitorServer};

export class MonitorServer extends EventEmitter {
   
   public somePublicFunction() {
      doSomeStuff();
   }
}
let monitor: MonitorServer;
function setMonitorServer(monitorServer: MonitorServer) {
    monitor = monitorServer;
}
router.get('/admin', (req, res) => {
    if (monitor) {
        monitor.somePublicFunction();
    }
})};