How to use socket.io in express routes?

前端 未结 2 1868
说谎
说谎 2020-12-02 13:31

I\'m using Express with Socket.io but I can\'t figure out how to use SocKet.io in Express routes.

I end up doing this in \"app.js\"

...
...

// devel         


        
2条回答
  •  温柔的废话
    2020-12-02 14:16

    You can use simple middleware before routes, and then use in res object

    app.use((req, res, next) => {
      res.io = io
      next()
    })
    

提交回复
热议问题