express.js 4 and sockets with express router

后端 未结 5 1789
灰色年华
灰色年华 2020-12-05 00:58

I\'m trying to create a really simple node API using express.js 4 but I need a few \'realtime\' events for which I added socket.io. I\'m fairly new to both so I\'m likely m

5条回答
  •  孤城傲影
    2020-12-05 01:50

    I think best way is to set io as a property of req, like below:

    app.use(function(req,res,next){
        req.io = io;
        next();
    });
    
    app.use('/your-sub-link', your-router);
    

提交回复
热议问题