Install node.js & npm (package manager) & nvm (version manager)
Ubuntu
sudo apt-get update
sudo apt-get install nodejs nodejs-legacy
sudo apt-get install npm
git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh
Add path to .bashrc
echo ". ~/.nvm/nvm.sh" >> ~/.bashrc
Amazon Linux
(?) sudo yum install gcc-c++ make
sudo yum install nodejs npm --enablerepo=epel
Websocket example
npm install websocket -gd
test.js :
var sys = require("sys"),
my_http = require("http");
my_http.createServer(function(request,response){
sys.puts("I got kicked");
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(9090);
sys.puts("Server Running on 9090");
Execute : node test.js
Browser : http://192.168.56.1:9090/
- -g : executable install globally
- -d : node_modules 安裝到
/usr/local/lib/node_modules/
目錄裡
- 在 .bashrc 設定
export NODE_PATH=/usr/local/lib/node_modules
- 以上是安裝在全域的 lib 需要的設定, 如果不加上
-gd
就會在目前目錄產生 node_modules
redis
install :
npm install redis -gd
SAVE
OR BGSAVE
指令, 快照儲存的路徑 : /var/lib/redis/dump.rdb
Install express web framework :
npm install express -gd
npm install -g express-generator
npm install -g ejs
Use (example: test):
express test --ejs
cd test
npm install (install dependencies)
npm start
- ejs 是一種 html template engine, 語法不像 default 的 jade 跟原生的 html 語法差很多,
- ejs 比較貼近原始的 html, 用法跟 php 的 template 很像
Install nodemon
檔案變動不用再重新啟動 node.js
安裝
npm install nodemon -g
啟動
cd test
nodemon ./bin/www
或
修改 `test/package.json` 將 `node ./bin/www` 改成 `nodemon ./bin/www`
ref
Packages
jshint : 檢查語法錯誤
npm install jshint -g
一個現成的套件提供介面看 log
建議安裝使用 root 安裝,比較不會遇到其他的麻煩 sudo npm install -g log.io --user="root"