"Node.js 是服務器端的 JavaScript 運行環境,它具有無阻塞(non-blocking)和事件驅動(event-driven)等的
特色,Node.js 采用 V8 引擎,同樣,Node.js 實現了類似 Apache 和 nginx 的web服務,讓你可以通過它來
搭建基於 JavaScript 的 Web App"
API
http://nodeapi.ucdok.com/api/
http://nodejs.org/api/
NPM
NPM是一個Node包管理和分發工具,已成為了非官方的發布Node模塊(包)的標准。有了NPM,可以快速找到特定服務要使用的包,進行下載,安裝以及管理已經安裝的包。
https://www.npmjs.org/
https://www.npmjs.org/browse/star
NPM常用命令
(1)npm install moduleNames
安裝Node模塊
如果在使用模塊的時候不知道其名字,可以通過http://search.npmjs.org網站按照索引值找到想要的模塊。npm也提供了查詢的功能,npm search indexName 安裝完畢后會產生一個node_modules目錄,其目錄下就是安裝的各個node模塊。node的安裝分為全局模式和本地模式。一般情況下會以本地模式運行,包會被安裝到和你的應用代碼統計的本地node_modules目錄下。在全局模式下,Node包會被安裝到Node的安裝目錄下的node_modules下。全局安裝命令為npm install -gd moduleName (npm install -g express)獲知使用 npm set global=true來設定安裝模式,npm get global可以查看當前使用的安裝模式。
(2)npm view moduleNames
查看node模塊的package.json文件夾
注意事項:如果想要查看package.json文件夾下某個標簽的內容,可以使用
npm view moduleName labelName
(3)npm list
查看當前目錄下已安裝的node包
注意事項:Node模塊搜索是從代碼執行的當前目錄開始的,搜索結果取決於當前使用的目錄中的node_modules下的內容。npm list parseable=true可以目錄的形式來展現當前安裝的所有node包
(4)npm help
查看幫助命令
(5)npm view moudleName dependencies
查看包的依賴關系
(6)npm view moduleName repository.url
查看包的源文件地址
(7)npm view moduleName engines
查看包所依賴的Node的版本
(8)npm help folders
查看npm使用的所有文件夾
(9)npm rebuild moduleName
用於更改包內容后進行重建
(10)npm outdated
檢查包是否已經過時,此命令會列出所有已經過時的包,可以及時進行包的更新
(11)npm update moduleName
更新node模塊
(12)npm uninstall moudleName
卸載node模塊
(13)一個npm包是包含了package.json的文件夾,package.json描述了這個文件夾的結構。
訪問npm的json文件夾的方法如下:
npm help json
此命令會以默認的方式打開一個網頁,如果更改了默認打開程序則可能不會以網頁的形式打開。
(14)發布一個npm包的時候,需要檢驗某個包名是否已存在
npm search packageName
(15)很多時候我們在使用一個npm包的時候經常會忘記了require其相互依賴的模塊,我們可以借助如如下命令來,查看此模塊,相互依賴的包都有哪些
更多命令請參看npm官方文檔 http://npmjs.org/doc/
國內NPM鏡像(更換國內鏡像)
npm config set strict-ssl false
npm config set registry http://registry.cnpmjs.org
npm install express -g
或http://registry.npm.taobao.org
http://npm.taobao.org/
VS配置NodeJS環境
我的環境是VS2013 + nodejs + nodejstools
codeplex: https://nodejstools.codeplex.com
express:https://github.com/visionmedia/express#quick-start
Express 錯誤(低版本)
doctype 5` is deprecated, you must now use `doctype html`
http://stackoverflow.com/questions/21102285/expressjs-node-app-running-error
DEMO
var http = require('http'); var port = process.env.port || 1337; //加載文件模塊 var fs=require('fs'); http.createServer(function (req, rep) { rep.writeHead(200, { 'Content-Type': 'text/plain' }); fs.readFile('package.json','utf-8',function(err,data){ if (err) { console.log(err); }else{ rep.write(data); rep.end('Hello World\n'); } }); }).listen(port); console.log('NodeJS Server running!');
Sublime Text配置
Sublime Text 3 : http://www.sublimetext.com/3
安裝插件管理器
菜單 View > Show Console 調出命令行工具粘貼回車
import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
安裝插件
快捷鍵Ctrl + Shift + P 輸入install,選擇Install Package回車,然后輸入nodejs選擇 NodeJs 安裝
常用的插件
https://sublime.wbond.net/packages/jQuery
https://sublime.wbond.net/packages/ConvertToUTF8
https://sublime.wbond.net/packages/Pretty%20JSON
https://sublime.wbond.net/packages/SublimeGit
https://sublime.wbond.net/packages/Nodejs
Ctrl+b 運行!
CentOS下配置開發環境
環境
CentOS 6.5 X64 min
下載安裝
#yum update
#yum install wget
#wget http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
#tar zxvf node-v0.10.26-linux-x64.tar.gz
配置環境變量
#vi /etc/profile
輸入
#set nodejs env
export NODE_HOME=/opt/node-v0.10.26-linux-x64
export PATH=$NODE_HOME/bin:$PATH
export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH
保存退出
#:wq
重啟生效
#source /etc/profile
檢查是否安裝成功
#node -v
#npm -v
源碼配置編譯安裝
yum -y install wget libtool automake autoconf gcc make gcc-c++ openssl-devel
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz
#tar zxvf node-v0.10.26.tar.gz
#cd /opt/node-v0.10.26
#./configure --prefix=/opt/node/v0.10.26
5分鍾左右
#make && make install
配置環境變量
#vi /etc/profile
輸入
#set nodejs env
export NODE_HOME=/opt/node/v0.10.26
export PATH=$NODE_HOME/bin:$PATH
export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH
重啟生效
#source /etc/profile
檢查是否安裝成功
#node -v
#npm -v
yum源安裝
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
yum install nodejs npm --enablerepo=epel
#安裝Express (已經升級,坑爹,折騰了我一晚上)
#npm install -gb express
express 已經把命令行工具分離出來了...
如果你要 Express 3
sudo npm install -g express-generator@3
express 4 的話
sudo npm install -g express-generatorUsage: express [options]
Options:
-h, --help output usage information
-V, --version output the version number
-e, --ejs add ejs engine support (defaults to jade)
-H, --hogan add hogan.js engine support
-c, --css add stylesheet support (less|stylus) (defaults to plain css)
-f, --force force on non-empty directory http://expressjs.com/guide.htm
Last login: Fri Apr 11 00:32:36 2014 from 192.168.0.104 [root@localhost ~]# vi /etc/profile # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you # are doing. It's much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. #set nodejs env export NODE_HOME=/opt/node/v0.10.26 export PATH=$NODE_HOME/bin:$PATH export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH "/etc/profile" 82L, 1932C written [root@localhost ~]# ls anaconda-ks.cfg install.log install.log.syslog [root@localhost ~]# source /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# node -v v0.10.26 [root@localhost ~]# npm -v 1.4.3 [root@localhost ~]# npm install -g express-generator npm http GET https://registry.npmjs.org/express-generator npm http 200 https://registry.npmjs.org/express-generator npm http GET https://registry.npmjs.org/express-generator/-/express-generator-4.0.0.tgz npm http 200 https://registry.npmjs.org/express-generator/-/express-generator-4.0.0.tgz npm http GET https://registry.npmjs.org/commander/1.3.2 npm http GET https://registry.npmjs.org/mkdirp/0.3.5 npm http 200 https://registry.npmjs.org/commander/1.3.2 npm http GET https://registry.npmjs.org/commander/-/commander-1.3.2.tgz npm http 200 https://registry.npmjs.org/mkdirp/0.3.5 npm http GET https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz npm http 200 https://registry.npmjs.org/commander/-/commander-1.3.2.tgz npm http 200 https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.5.tgz npm http GET https://registry.npmjs.org/keypress npm http 200 https://registry.npmjs.org/keypress npm http GET https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz npm http 200 https://registry.npmjs.org/keypress/-/keypress-0.1.0.tgz /opt/node/v0.10.26/bin/express -> /opt/node/v0.10.26/lib/node_modules/express-generator/bin/express express-generator@4.0.0 /opt/node/v0.10.26/lib/node_modules/express-generator 忖?mkdirp@0.3.5 忖?commander@1.3.2 (keypress@0.1.0) [root@localhost opt]# express app -e create : app create : app/package.json create : app/app.js create : app/public create : app/public/javascripts create : app/public/images create : app/public/stylesheets create : app/public/stylesheets/style.css create : app/routes create : app/routes/index.js create : app/routes/users.js create : app/views create : app/views/index.ejs create : app/views/error.ejs create : app/bin create : app/bin/www install dependencies: $cd app && npm install run the app: $DEBUG=my-application ./bin/www [root@localhost ~]# express -V 4.0.0 [root@localhost opt]# cd app/ [root@localhost app]# npm install
關閉防火牆
#service iptables stop
永久關閉防火牆
#chkconfig iptables off
查看防火牆關閉狀態
#service iptables status
1)永久性生效,重啟后不會復原
開啟: chkconfig iptables on
關閉: chkconfig iptables off
編譯配環境變量
#vi /etc/sysconfig/network-scripts/ifcfg-eth0
#service network restart
#yum install yum-fastestmirror
#yum clean all
#yum update
2)即時生效,重啟后復原
開啟: service iptables start
關閉: service iptables stop
開啟Node服務
express 4 默認生成的代碼 app.js貌似沒有監聽端口,已經放在./bin/www中
#!/usr/bin/env node var debug = require('debug')('my-application'); var app = require('../app'); app.set('port', process.env.PORT || 3000); var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); });
#node node ./bin/www
IntelliJ IDEA 中環境配置
安裝插件 http://www.jetbrains.com/idea/features/nodejs.html
檢查環境變量
調試選項
目錄結構
調試程序
WebStorm(與IDEA差不多,沒有深入研究)
Eclipse配置NodeJS開發環境
安裝nodeclipse插件(選擇需要的包)
http://www.nodeclipse.org/updates/
google V8調試插件(可能需要牆)
http://chromedevtools.googlecode.com/svn/update/dev/
查看Node配置信息
新建Express項目(就是默認的版本太低了,4.x API 與目錄結構都變了)
Debug調試
2014年4月4日 發布 NTVS 1.0 Beta 貌似不能斷點調試,已經切換回老版本
2014年4月7日 使用 Sublime Text 很好用
2014年4月10日 使用了幾天,還是把開發環境搬到Liunx來了,環境CentOS6.5 + Gedit + Sublime Text + Firefox
2014年4月23日 使用 IntelliJ IDEA 非常好用
2014年4月24日 使用 Eclipse配置NodeJS開發環境 缺點Express版本低,IDEA新建項目可以手動選擇版本
Refer:
http://nodejstools.codeplex.com/documentation
http://www.hanselman.com/blog/IntroducingNodejsToolsForVisualStudio.aspx
NPM registry 管理工具
http://cnodejs.org/topic/5326e78c434e04172c006826
快速搭建 Node.js 開發環境以及加速 npm
http://cnodejs.org/topic/5338c5db7cbade005b023c98
阿里雲主機Nginx下配置NodeJS、Express和Forever
http://cnodejs.org/topic/5059ce39fd37ea6b2f07e1a3
加快npm的下載速度
http://cnodejs.org/topic/53330242edf0031c2c00ca81
請教,Nodejs 開發,用神馬IDE呢?
http://cnodejs.org/topic/4f32389669bab4d6760389c7#53929c94c3ee0b5820352451



