Node.js和Socket.io-无法读取属性';未定义的套接字

Node.js和Socket.io-无法读取属性';未定义的套接字,node.js,Node.js,我对node.js还是个新手,希望你能帮助我 我使用带有模块express(4.15.2)和socket.io(1.7.3)的Node(v7.9.0)创建一个简单的Web服务器,并将事件从Web服务器发送到连接的客户端 我正在尝试模块化我的应用程序,并希望在不同的js文件上向客户端发出不同的事件 现在这是我当前的代码: ---start.js: // Configure the build-in webserver var express = require('express'), a

我对node.js还是个新手,希望你能帮助我

我使用带有模块express(4.15.2)和socket.io(1.7.3)的Node(v7.9.0)创建一个简单的Web服务器,并将事件从Web服务器发送到连接的客户端

我正在尝试模块化我的应用程序,并希望在不同的js文件上向客户端发出不同的事件

现在这是我当前的代码:

---start.js:

// Configure the build-in webserver
var 
  express = require('express'),
  app = express(),
  server = require('http').createServer(app),
  io = require('socket.io').listen(server),
  conf = require('./serverConfig.json');

module.exports = server;            // Export to use with uart_functions.js

module.exports.io = io;             // Export to use with uart_functions.js

server.listen(conf.port);           // Configure the server to port 8080

app.use(express.static(__dirname + '/pages'));  // All the website data is in this folder

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/pages/index.html');      // Webserver should return the page index.html
});

console.log("Server is opened: http://127.0.0.1:" + conf.port +"/\n");

io.sockets.on('connection', function(socket) {
    console.log("Verbunden!!!\n");
    io.sockets.emit('chat', {zeit: new Date(), name: "Server", text: "Du bist nun mit dem Server verbunden!"}); // Send Data to Client. This works
});
---uart_functions.js:

var ioSocket = require('./start.js').io;

function TelegrammID1() {
    console.log("RUN ID 1\n");
    ioSocket.sockets.emit('chat', {zeit: new Date(), name:'Tester', text: "Das ist ein Test"}); // Send data to client - This doesn't work    
}
现在在文件uart_functions.js中我得到了错误:“TypeError:无法读取未定义的属性‘sockets’。”

我理解为什么会发生错误。但我不知道我还需要通过module.export将start.js文件传递到uart_functions.js文件中

关于搜索,我发现了以下内容:

不幸的是,没有有用的答案。
谢谢你的帮助

我不太确定你的做法是否正确。按如下方式导出它

    // Configure the build-in webserver
    var 
      express = require('express'),
      app = express(),
      server = require('http').createServer(app),
      io = require('socket.io').listen(server),
      conf = require('./serverConfig.json');

   // module.exports = server;            // Export to use with uart_functions.js

   // module.exports.io = io;             // Export to use with uart_functions.js

    server.listen(conf.port);           // Configure the server to port 8080

    app.use(express.static(__dirname + '/pages'));  // All the website data is in this folder

    app.get('/', function(req, res) {
        res.sendfile(__dirname + '/pages/index.html');      // Webserver should return the page index.html
    });

    console.log("Server is opened: http://127.0.0.1:" + conf.port +"/\n");

    io.sockets.on('connection', function(socket) {
        console.log("Verbunden!!!\n");
        io.sockets.emit('chat', {zeit: new Date(), name: "Server", text: "Du bist nun mit dem Server verbunden!"}); // Send Data to Client. This works
    });

    module.exports = {
     io: io,
     server:server
    }
谢谢你的帮助

使用此代码:

  module.exports = 
  {
    io: io,
    server:server
  }
uart_functions.js中的这一行:
var ioSocket=require('./start.js')
我发现以下错误:“TypeError:无法读取未定义的属性'emit'”

我还尝试了以下导出:

module.exports = 
  {
    io: io,
    server:server,
    app: app,
    express: express
  }

但这会返回相同的错误….

问题在于,您需要在函数
telegramid1
之外使用
ioSocket
,该函数在连接时再次导出以运行。在函数
telegramid1
中定义变量
ioSocket
,并确保导出该函数

 function TelegrammID1() {
   var ioSocket = require('./start.js').io;
   console.log("RUN ID 1\n");
   ioSocket.sockets.emit('chat', {zeit: new Date(), name:'Tester', text: "Das ist ein Test"}); // Send data to client - This doesn't work
}

太棒了@Rohith,我从来没想过这会引起问题。为我工作!谢谢