Reputation: 11
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.
Upvotes: 0
Views: 1413
Reputation: 10569
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.
Upvotes: 1