Node.js 快速路由器中的Pass和modifiy类传递
我想将一个类传递给express router,并根据每个请求修改我的类 server.tsNode.js 快速路由器中的Pass和modifiy类传递,node.js,express,class,express-router,Node.js,Express,Class,Express Router,我想将一个类传递给express router,并根据每个请求修改我的类 server.ts import routes from './routes'; import Client from './client'; class Server { constructor () { this.app = express() this.client = new Client(); this.init() } init() {
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))
}
}
路线
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();
}
}
路线1.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;
你通过的课程在哪里?你期望得到什么结果?