无法调用对象未定义的方法 'on'
cannot call method 'on' of undefined at object
当我在 node.js 命令提示符中尝试 运行 节点 app.js 时,我收到一条错误消息
cannot call method 'on' of undefined at object
这是我的代码:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
io.socket.on('connection', function(socket) {
socket.on('send message', function(data){
io.socket.emit('new message', data);
});
});
出了什么问题?
io.socket.on
应该是 io.on
.
io.socket.emit
应该是 socket.emit
.
如:
io.on('connection', function(socket) {
socket.on('send message', function(data){
socket.emit('new message', data);
);
});
当我在 node.js 命令提示符中尝试 运行 节点 app.js 时,我收到一条错误消息
cannot call method 'on' of undefined at object
这是我的代码:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
io.socket.on('connection', function(socket) {
socket.on('send message', function(data){
io.socket.emit('new message', data);
});
});
出了什么问题?
io.socket.on
应该是io.on
.io.socket.emit
应该是socket.emit
.
如:
io.on('connection', function(socket) {
socket.on('send message', function(data){
socket.emit('new message', data);
);
});