我想听连接事件(带socket.io的nodejs)

时间:2019-10-11 00:46:26

标签: javascript node.js express socket.io ejs

我正在Express中使用Node js 我有app.js主文件 和router / index.js

我有主连接,端口监听app.js

const express = require('express');
const app = express();

var server=app.listen(3000);


var indexRouter = require('./routes/index');
var ioRouter = require('./routes/io');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(express.static('public', options));

app.use('/', indexRouter); //  call index.js
app.use('/io', ioRouter);

和这个index.js

const dotenv = require('dotenv').config();
const express = require('express');
var router = express.Router();


router.get('/', (req, res) => {
    res.render('index', {result: "result"});
});

module.exports = router;

我想用index.js监听此事件

io.on('connection', (socket) => {
    console.log("Hello");
});

0 个答案:

没有答案