最近在学 Nodejs,将自己的学习过程记录在这里,希望可以帮到将要学习 Nodejs 的同学。
因为现在安装 Ubuntu 也非常方便,所以我的 Platform 是 Ubuntu 。
其他 Platform 的同学可以到这里看:http://howtonode.org/how-to-install-nodejs
一:安装nodejs:
Nodejs官方网站:http://nodejs.org/
Nodejs Github网址:https://github.com/joyent/node
git clone git://github.com/joyent/node.git
cd node
./configure
make
sudo make install
但是这样安装的node版本一般都是最新版本的,有可能不是稳定版本(stable versions),如果不是稳定版本,那么在后面安装 Express 的时候就比较麻烦。
所以建议安装node最好是选择安装最新的稳定版本,比如:2011.06.29 v0.4.9 (stable)
官方安装Wiki:https://github.com/joyent/node/wiki/Installation
二:安装npm
NPM官方网站:http://npmjs.org/
NPM Github网址:https://github.com/isaacs/npm
One Line Install:
curl http://npmjs.org/install.sh | sh
或者sudo curl http://npmjs.org/install.sh | sh
但是我没有安装成功会报错,截取一段贴出来:
make: * [install] 错误 13
npm WARN Not installed in /usr/local/lib/node_modules npm
npm ERR! Could not create /usr/local/lib/node_modules/___npm.npm
npm ERR! error installing npm@1.0.15 Error: EACCES, Permission denied ‘/usr/local/lib/node_modules’
npm ERR! Error: EACCES, Permission denied ‘/usr/local/lib/node_modules’
npm ERR!
然后我就用普通的方法安装了:
git clone http://github.com/isaacs/npm.git cd npm sudo make install
三:安装Express
Express官方网站:http://expressjs.com/
Express Github官方网址:https://github.com/visionmedia/express
sudo npm install -g express
安装后目录并不在/home/huangyaowu下,
而是在:/usr/local/bin/下,
huangyaowu@ubuntu:~$ sudo npm install -g express [sudo] password for huangyaowu: /usr/local/bin/express -> /usr/local/lib/node_modules/express/bin/express express@2.4.3 /usr/local/lib/node_modules/express ├── mime@1.2.2 ├── connect@1.6.0 └── qs@0.3.0 huangyaowu@ubuntu:~$
运行个例子:
现在可以在/user/huangyaowu目录下执行:
express huang
会新建一下目录:
huangyaowu@ubuntu:~$ express huang create : huang create : huang/package.json create : huang/app.js create : huang/public/javascripts create : huang/public/images create : huang/views create : huang/views/layout.jade create : huang/views/index.jade create : huang/public/stylesheets create : huang/public/stylesheets/style.css huangyaowu@ubuntu:~$
然后再cd huang到huang的目录下执行:
npm install -d
完成后执行:
node app.js
出现这句后:
Express server listening on port 3000 in development mode
在浏览器输入:http://localhost:3000/
四:安装其他
安装jsdom:https://github.com/tmpvar/jsdom
jsdom安装上了,但是在运行的时候说:Cannot call method 'runInContext' of undefined
解决办法:https://github.com/tmpvar/jsdom/commit/4cf155a1624b3fb54b2eec536a0c060ec1bab4ab
npm installhttps://github.com/tmpvar/jsdom/tarball/4cf155a1624b3fb54b2eec536a0c060ec1bab4ab
如果还是不行:
git clone http://github.com/tmpvar/jsdom.git cd jsdom npm link
使用的时候一定要注意jsdom的目录,应用的是/lib/jsdom var jsdom = require('../../node_modules/jsdom/lib/jsdom'), window = jsdom.jsdom().createWindow(); jsdom.jQueryify(window, 'jquery.js', function() { window.jQuery('body').append('<div>hello node</div>'); console.log(window.jQuery('.testing').text()); });
安装htmlparser:https://github.com/tautologistics/node-htmlparser
npm install htmlparser
loading