0

Ok so I'm basically having the same problem as this. But the answers given there don't work for me. So let me re-explain my problem. First of all, here's my code:
Server-Side Javascript (app.js)

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


Client-Side Javascript (client.js)

3: var socket = (s)io.connect('http://localhost:3000');
//the (s) represents testing with and without the s out of desperateness :)

Client-Side Template (Jade)

script(src='/socket.io/socket.io.js')
script(src='/javascripts/client.js')

So from what I've read it seems like socket.io should be handling putting the socket.io.js file there but I'm guessing that I have something configured wrong because it's not doing that. Anyways the errors I get with this are:

GET http://localhost:3000/socket.io/socket.io.js 404 (Not Found)
Uncaught ReferenceError: io is not defined - client.js line 3

After doing some research it seemed that I could change the jade file to link directly to a stable file. So by changing my code to this:
Client-Side Template (Jade)

script(src='http://cdn.socket.io/stable/socket.io.js')
script(src='/javascripts/client.js')

Then the error that I get from this is:

Uncaught TypeError: Object #<Object> has no method 'connect' - client.js line 3

I've been trying to figure this out for hours now. And it seems that I need to have socket.io-client so I made sure that it is installed. I dunno if this will but I am using Express.js as well and I will give you the layout of my files.
/project

app.js  /node_modules  package.json  /public  /routes  /views

/project/node_modules

/connect  /express  /jade  /jquery  /socket.io  /stylus

/project/node_modules/socket.io

/benchmarks  index.js  Makefile       package.json  restrict_jsonp.patch
History.md   /lib      /node_modules  Readme.md

/project/node_modules/socket.io/node_modules

/policyfile  /redis  /socket.io-client

/project/public

/images  /javascripts  /jquery  /stylesheets

/project/public/javascripts

client.js

Anyways, any help would be greatly appreciated! :)


As Felix Loether pointed out, the API for Express changed from 2.* to 3.* After spending way too many hours trying to figure out the best way to re-work my code I decided to re-install express to an earlier version by doing this:

npm install express@2.5.8 -g

There is a lot more support (as of today) for Express 2.* so as I am still learning it's better for me to use the earlier version.

4

1 回答 1

1

正如 Felix Loether 指出的那样,Express 的 API 从 2.* 更改为 3.* 在花了太多时间试图找出重新编写代码的最佳方法之后,我决定通过以下方式将 express 重新安装到早期版本这样做:

npm install express@2.5.8 -g

Express 2.* 有更多支持(截至今天),因此我仍在学习,最好使用早期版本。

于 2012-07-31T21:47:20.717 回答