Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

node.js - Node server, Socket.io 'io is not defined'?

I've followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder. It appears that the socket.io client-side javascript file isn't being found.

(I've looked at other peoples fixes, which is to include the JavaScript file in a script tab. I have not had to do this for my previous node + socket.io projects).

JavaScript on client:

var socket = io.connect('http://localhost');

JavaScript on server:

var io = require('socket.io').listen(app);

node_modules folder:

socket.io, which has an internal node_modules folder containing socket.io-client

Error Message:

Uncaught ReferenceError: io is not defined
(anonymous function)

When I include the socket.io client manually: http://cdn.socket.io/stable/socket.io.js

I get a different error which is:

Uncaught TypeError: Object #<Object> has no method 'connect'
(anonymous function)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On the client, did you do:

<script src="/socket.io/socket.io.js"></script>

before you set the socket variable?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...