nodejs Doubt about module.Exports

Asked

Viewed 74 times

0

Good evening. I have a question maybe a little bit simple but that is keeping me up at night and I think I’m doing it the wrong way. However, if someone can correct me and explain why I made a mistake I will be grateful.

I have a file (module) of my socket io which receives some data via parameter. However, if I give a console in these parameters out of do io.on('Connection'.. returns to me everything according to what I sent... If I try to return inside io.on('Connection returns me null or Undefined.. Pq? I don’t understand.

Below is an excerpt of the code

module.exports = (io, path, method, status) => {

    console.log(path, method, status) // AQUI FUNCIONA O QUE RECEBI NESTE MODULO

    let users_online = 0
    io.on('connection', (socket) => {
        console.log(path, method, status) // AQUI DENTRO JA ME RETORNA UNDEFINED
        users_online++
        io.emit('users_online', users_online)

        socket.on('disconnect', () => {
            users_online--
            io.emit('users_online', users_online)
        })
    })
}

What would be the solution? :(

  • It makes no sense for the inner console to be helpless. You can create an example of the problem here or at https://codesandbox.io?

  • So, Sergio, basically I have the app.js at the root with the following codigo 
app.use((req, res, next) => {
 let route = req.url
 let method = req.method
 res.on('finish', () => {
 ws(io, route, method, res.statusCode)
 })
 next()
})
 and that middleware q fires into the socket code that is in config/Ws.js :(

  • anyway: https://github.com/rpelizza/socket-middleware

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.