传修改class 传入express router

Pass and modifiy class passing in express router

我想将 class 传递给 express 路由器,并为每个请求修改我的 class。

server.ts

import routes from './routes';
import Client from './client';

class Server {
    constructor () {
        this.app = express()
        this.client = new Client();
        this.init()
    }

    init() {
        this.app.use(routes(this))
    }
}

routes.ts

import {Router} from 'express';

export default (api) => {
    const router = Router();
    router.get('/', (req, res) => {
        api.client.size+=1;
        console.log(api.client.size);
    });
    return router;
}

类似于这个问题:

但是这段代码不起作用。

有人有想法吗?

谢谢

最后,这是我的问题的答案:

server.js

class Server {
    constructor () {
        this.app = express();
        this.app.locals.clients = new Clients();
    }
}

route.ts

import {Router} from 'express';

const router = Router();

router.get('/', (req, res) => {
    res.app.locals.client.size += 1;
    res.send(`bla bla bla`);
});

export default router;