【问题标题】:io is not defined in nodejsio 没有在 nodejs 中定义
【发布时间】:2017-06-26 14:11:30
【问题描述】:

我的 io 未定义,这可能是什么原因?

arduino.js

var debug = require('debug')('arduino');
var five = require('johnny-five');
var socket = io();

index.js

var debug = require('debug')('server');
var express = require('express');
var http = require('http');
var socketIo = require('socket.io');
var app = express();
var httpServer = http.createServer(app);
var io = socketIo(httpServer);

可能是什么问题?

【问题讨论】:

  • arduino.js和index.js是什么关系?

标签: javascript node.js io socket.io


【解决方案1】:

ioindex.js 模块中的局部变量。

arduino.js 是一个不同的模块。

它似乎没有使用index.jsindex.js 似乎也没有使用它。即使是这种情况,io 也是一个未导出的局部变量,因此在定义它的模块之外不可用。

【讨论】:

  • 我如何定义 io() 使其在两个模块中相同,并且事件相互监听
  • 更熟悉 Socket.io 的人可能会有更好的答案,但如果你想在模块之间传递 io 变量,你可以这样做:在index.js:exports.io = io;,然后在arduino.js: const io = require('index').io;.
猜你喜欢
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多