您的位置:首页 > Web前端 > Node.js

node.js学习-整理

2012-09-13 23:53 399 查看
安装node.js:
去这里http://nodejs.org/download/选择Source Code下载,$ ./configure && make && sudo make install

如果./configure出现error,

请确保:python 2.6 or 2.7 , GUN make 3.81 or newer.
然后make如果出错如下:
node-v0.8.***/out/Release/linker.lock: No such file or directory
原因是【猜测】:linux服务器的内核版本太老.

必备+常用包:

$ sudo npm install -gsupervisor //可以监控文件的修改从而自动重新加载js.
$ sudo npm install socket.io //非常不错的websocket封装包.没有之一。
$ sudo npm install -gforever //使node服务器以deamon方式运行.

如果服务器不能联网,怎么在其上安装forever?
这样:https://github.com/nodejitsu/forever/ 点击文件列表右上方的Downloads按钮,即进入https://github.com/nodejitsu/forever/downloads, 选择"Download as tar.gz", 下下来了文件名为nodejitsu-forever......tar.gz, 扔到服务器上然后执行命令:$ sudo
npm install -g
nodejitsu-forever......tar.gz
file:///....这种。因为后者对于某
装websocket包:

$ sudo npm install websocket-server<--坑爹!
经折腾,这个包就是一个坑爹!

这个好:file:///....这种。因为后者对于某
$ sudo npm install socket.io

很好的node入门,非常适合我的口味:
http://www.nodebeginner.org/index-zh-cn.html

websocket与node.js的完美结合:(看看了解下也就罢了。这里面用到的包"websocket-server"非常坑爹、千万别用)

http://cnodejs.org/topic/4f16442ccae1f4aa27001139

socket.io:

http://socket.io/
http://www.cnblogs.com/wei2yi/archive/2011/03/23/1992830.html

nodejs服务端和web前端共用一个js文件(厉害. 一开始我的前端用不成,后来找到原因了:是由于我的router是自己写的,没有对这个js文件进行路由。。。):):
/article/4885312.html
也可以看看可以看看node_modules/socket.io/node_modules/socket.io-client/lib/io.js这个文件。同样的。目测更diao。



介绍怎么使得shell脚本可以deamon方式运行,然后给出了一个让node服务器以后台deamon方式运行的包“forever”: http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
我的记录

https & wss:

1.产生证书:
$ mkdir certs
$ openssl req -new -x509 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes
随便输入几个name...
$ ls certs/

2. node.js里用https模块:
var options =
{
key: fs.readFileSync("certs/key.pem"),
cert: fs.readFileSync("certs/cert.pem")
};
httpsServer = https.createServer(options, onRequest);

3.客户端链接相应的换成https即可:
var socket = io.connect(
//note: here can be 'http'. but, if the secure = true in the down, it's https really~. if you set 'https', the down can delete.
"http://" + location.hostname + ":" + location.port + "/chat" //'chat' is 'namespace' in socket.io server.
, {secure: true} //if true, it's https and wss.
);
具体可以看看node_modules/socket.io/node_modules/socket.io-client/lib/io.js这个文件。

4. error

我在linux下用Chromium调试:file:///home/U/socketServer/test.html, 报错如下:(firefox倒是没有报错而挺好)

XMLHttpRequest cannot load https://localhost:8100/socket.io/1/?t=1350895909345. Origin null is not allowed by Access-Control-Allow-Origin.

网上有说Chromium打开file会出问题,好吧,我把客户端测试html扔到本机nginx服务器上,结果还是报错:

GET https://localhost:8100/socket.io/1/?t=1351492517703

Socket.handshakesocket_io.js:1659

Socket.connectsocket_io.js:1699

Socketsocket_io.js:1551

io.connectsocket_io.js:94

(anonymous function)

具体到代码‘xhr.send(null);’。
崩了个溃的。网上有说是xhr不可以跨域,我不懂。进过反复排查,最终把服务端的server创建的ip改为具体的ip(之前用的是"0.0.0.0").然后就可以在别的机器上通过我扔到nginx上的html来进行websocket通讯了。然而这样本地却连firefox都无法io.connect了。。好吧,总结下:本地调试环境中([b]I'm working in my linux),就用firefox吧,服务端创建server的ip和客户端io.connect的ip都设为0.0.0.0,直接firefox打开本地html(file:///)即可联调;而如果是发布生产环境,那就把server的ip和客户端io.connect的ip都设为具体的ip吧(10.11.12.13),只不过这时就没法本地测试了.
补充: 如果是https且证书不是合法的,那就必须先在浏览器上访问一下该https从而把信任这个不合法的证书,否则报错如上。(我怀疑就只是file:///和证书的原因。。)[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: