在MVC Express应用程序中使用soket.io模块时出错

时间:2019-01-02 14:10:47

标签: node.js socket.io

我想在我的应用中使用soket.io,并在mvc express应用中使用module.export

可能的服务器代码为:

var express = require('express')
  , app = express()
  , router = express.Router();
var http= require('http').Server(app);
var io=require('socket.io')(http);
app.io = require('socket.io')();
...
//api
app.use(require('./controllers'));
app.listen(config.port, function() {
    console.log('Listening on port ' + config.port)})

我的控制器/索引:

var express = require('express')
    , router = express.Router();
..
router.get('/', function(req, res) {
    res.send('hi')
});
router.use('/chat', require('./chat'));
module.exports = router
</code>

我的chat.js文件是:

var app = require('express')
    , chat = app.Router();
var http= require('http').Server(app);
var io = require('socket.io')(http);
var path = require("path");

chat
    .get('/', function(req, res) {

        let reqPath = path.join(__dirname, '../');
       // res;
        io.on('connection', function(socket){
            console.log('a user connected');
        })
        res.sendFile(reqPath +'/views/index.html');    })
io.on('connection', function(socket){
    console.log('a user connected');
});

代码发送我的html文件并显示我的结果。但不要在控制台中显示console.log('a user connected');

1 个答案:

答案 0 :(得分:0)

$ echo 'play allegro here' | rg -w leg
$ echo 'I have a leg.' | rg -w leg
I have a leg.