简体   繁体   中英

socketio io.on is not a function

I have seen many solutions on the web but they didn't help me much.

I have created a working nodejs authentication system in express.js framework. i just want to add a chat function that help us to get the information about it .

the route is

const dashboard = require('./routes/dashboard').dash;

and the calling function is

app.use('/dashboard', dashboard)

on this code page ./rotes/dashboard.js i'm exporting them like this

const express = require('express');
const router = express.Router();
var app = express();
const io = require('socket.io');
const fs = require('fs');

const { ensureAuthenticated } = require('../db/auth.js');


//exporting dashboard 
module.exports = {
  dash: router.get('/', (req,res)=>{
    var chat = io.on('connection', (socket)=>{
      console.log('chat is connected');
    })
    res.render('dashboard', {chat:chat});

  })
}

but i'm getting this error:

io.on is not a function
TypeError: io.on is not a function
    at E:\expressBasedLOginSignUP\routes\dashboard.js:13:19
    at Layer.handle [as handle_request] (E:\expressBasedLOginSignUP\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\expressBasedLOginSignUP\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\expressBasedLOginSignUP\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\expressBasedLOginSignUP\node_modules\express\lib\router\layer.js:95:5)
    at E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:335:12)
    at next (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:174:3)
    at router (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (E:\expressBasedLOginSignUP\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:317:13)
    at E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:335:12)
    at next (E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:275:10)
    at E:\expressBasedLOginSignUP\node_modules\express\lib\router\index.js:635:15

someone help me how to avoid this function and why this is happening.

socket.io returns a function, you have to call the function and pass your http server reference to it.

Example:

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);

And then you can call events on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM