npm,pm2等相關知識的學習

如今開始接手node端測試,有好多知識點,好比啓動進程的命令,查看進程的命令都不是很清晰,如今具體來學習下~css

npm由來

前端最大的社區是GitHub,你們在這裏分享代碼,討論問題,收集學習資源。你們須要依賴什麼安裝包從之前的去官網下載到去GitHub下載,仍是很麻煩。這時候一個擁有三大美德的程序員 Isaac Z. Schlueter (如下簡稱 Isaaz)給出一個解決方案:用一個工具把這些代碼集中到一塊兒來管理吧!html

這個工具就是他用 JavaScript (運行在 Node.js 上)寫的 npm,全稱是 Node Package Manager前端

NPM 的實現思路大概是這樣的:node

  1. 買個服務器做爲代碼倉庫(registry),在裏面放全部須要被共享的代碼
  2. 發郵件通知 jQuery、Bootstrap、Underscore 做者使用 npm publish 把代碼提交到 registry 上,分別取名 jquery、bootstrap 和 underscore(注意大小寫)
  3. 社區裏的其餘人若是想使用這些代碼,就把 jquery、bootstrap 和 underscore 寫到 package.json 裏,而後運行 npm install jquery,npm 就會幫他們下載代碼
  4. 下載完的代碼出如今 node_modules 目錄裏,能夠隨意使用了。

這些能夠被使用的代碼被叫作「包」(package),這就是 NPM 名字的由來:Node Package(包) Manager(管理器)。node包管理器。python

npm的應用

新版的nodejs 已經集成了npm,因此安裝nodejs的時候,npm也一併安裝好了。mysql

經常使用命令:react

安裝模塊:npm install <module name>jquery

卸載模塊:npm uninstall <module name>webpack

更新模塊:npm update <module name>git

package.json...

在 NodeJS 項目中,會看到一個package.json,這個文件用來聲明項目中使用的模塊,這樣在新的環境部署時,只要在 package.json 文件所在的目錄執行npm install命令便可安裝所須要的模塊。

還有一些scripts腳本,能夠執行一些命令,好比啓動npm start

{
  "name": "maimai_node",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "DEBUG='app,body,rpc' gulp watch",
    "pc": "DEBUG='app,body,rpc' gulp watch_pc",
    "test": "DEBUG='app' mocha test/main.js --harmony -t 200000",
    "dev": "DEBUG='' gulp watch",
    "reload": "DEBUG='app,body,rpc' node --harmony --inspect app.js",
    "dev:allocatememory": "DEBUG='app,body,rpc' gulp --max_old_space_size=4098 watch",
    "growth": "DEBUG='app,body,rpc' gulp --gulpfile gulpfile.growth.js --max_old_space_size=4098 watch",
    "build-lib": "node-gyp rebuild --directory native_lib",
    "build-bundle": "webpack --config webpack.config.js --progress --display-optimization-bailout",
    "lint": "CHANGED_LIST=`git diff-index --name-only HEAD | grep .js$`; if [[ $CHANGED_LIST ]]; then ./node_modules/.bin/eslint --fix `echo $CHANGED_LIST | xargs`; fi",
    "precommit": "#npm run lint",
    "watchNoBundle": "DEBUG='app,body' DEV_PATH=dev gulp --max_old_space_size=4098 watchNoBundle"
  },
  "dependencies": {
    "autoprefixer": "6.0.3",
    "autoprefixer-core": "6.0.1",
    "babel": "6.5.2",
    "babel-eslint": "^8.0.3",
    "babel-plugin-transform-async-to-generator": "6.8.0",
    "babel-polyfill": "6.9.1",
    "babel-preset-es2015": "6.9.0",
    "babel-preset-react": "6.11.1",
    "babel-preset-stage-0": "6.5.0",
    "babel-register": "6.9.0",
    "babelify": "6.4.0",
    "baobab": "2.0.1",
    "base62": "1.1.0",
    "bindings": "1.2.1",
    "blob-util": "2.0.2",
    "bluebird": "3.0.5",
    "brfs": "1.4.1",
    "bunyan": "1.8.2",
    "canvas": "1.6.5",
    "classnames": "^2.1.3",
    "co": "4.6.0",
    "co-child-process": "0.0.3",
    "co-mocha": "1.1.2",
    "co-request": "1.0.0",
    "co-wechat-api": "3.8.2",
    "co-wechat-oauth": "2.0.1",
    "commander": "2.9.0",
    "crc-32": "1.0.1",
    "crypto-js": "^3.1.9-1",
    "cssmin": "0.4.3",
    "cuint": "0.2.0",
    "date-utils": "1.2.17",
    "debug": "2.2.0",
    "detect-browser": "1.1.1",
    "dtrace-provider": "0.8.5",
    "echarts": "^4.0.4",
    "echarts-for-react": "^2.0.8",
    "email-validation": "0.1.2",
    "emoji-strip": "^1.0.1",
    "envify": "3.4.0",
    "es6-promise": "3.0.2",
    "event-stream": "3.3.2",
    "eventemitter3": "^3.1.0",
    "exenv": "^1.2.2",
    "fastclick": "^1.0.6",
    "find-remove": "1.0.1",
    "form-data": "1.0.1",
    "generic-pool": "2.2.0",
    "gulp": "3.9.1",
    "gulp-cssmin": "0.1.7",
    "gulp-download": "0.0.1",
    "gulp-imacss": "1.0.0",
    "gulp-jade": "1.1.0",
    "gulp-less": "3.0.3",
    "gulp-nodemon": "2.0.4",
    "gulp-postcss": "6.0.1",
    "gulp-rename": "1.2.2",
    "gulp-replace": "0.5.4",
    "gulp-streamify": "1.0.2",
    "gulp-uglify": "1.4.2",
    "gulp-util": "3.0.6",
    "gulp-watch": "4.3.5",
    "gulp-zip": "3.1.0",
    "heapdump": "0.3.7",
    "highcharts": "^6.0.7",
    "hls.js": "0.8.6",
    "html2canvas": "^1.0.0-alpha.12",
    "http-shutdown": "1.2.0",
    "idcard": "4.1.0",
    "imacss": "1.0.0",
    "immutability-helper": "^2.9.0",
    "in-view": "0.6.0",
    "ioredis": "2.3.0",
    "iscroll": "5.2.0",
    "jade": "1.11.0",
    "jade-loader": "0.8.0",
    "jquery": "^3.3.1",
    "js-base64": "2.3.2",
    "json-loader": "0.5.4",
    "jsonfile-promised": "0.0.1",
    "jsonwebtoken": "5.4.0",
    "klass": "1.4.1",
    "koa": "1.2.4",
    "koa-body": "1.4.0",
    "koa-compose": "2.4.0",
    "koa-cors": "0.0.16",
    "koa-csrf": "2.3.0",
    "koa-gzip": "0.1.0",
    "koa-jade": "2.0.0",
    "koa-mount": "1.3.0",
    "koa-proxy": "0.4.1",
    "koa-qs": "2.0.0",
    "koa-rewrite": "1.1.0",
    "koa-router": "5.2.3",
    "koa-send": "3.1.0",
    "koa-session": "3.3.1",
    "koa-static": "1.4.9",
    "koa-swig": "2.1.0",
    "koa-views": "3.1.0",
    "lodash": "^4.17.10",
    "md5": "^2.2.1",
    "md5-file-promise": "1.0.2",
    "mobile-detect": "1.3.1",
    "mocha": "2.3.3",
    "modern-syslog": "^1.1.4",
    "moment": "2.10.6",
    "moment-mini": "2.18.1",
    "mongoose": "4.4.12",
    "mongoose-auto-increment": "5.0.1",
    "mongoose-multitenant": "0.8.2",
    "ms": "0.7.1",
    "mysql": "2.14.1",
    "nan": "2.6.2",
    "nconf": "0.8.2",
    "node-canvas-text": "1.0.2",
    "node-fetch": "1.7.1",
    "node-jsx": "0.13.3",
    "node-rsa": "0.2.26",
    "node-xml-lite": "0.0.5",
    "nsqjs": "0.8.4",
    "opentype.js": "0.7.1",
    "pako": "0.2.8",
    "platform": "^1.3.4",
    "preload-js": "^0.6.3",
    "prop-types": "15.6.0",
    "protobufjs": "6.8.0",
    "qiniu": "7.2.1",
    "qrcode.react": "^0.8.0",
    "qs": "6.5.1",
    "radium": "^0.14.2",
    "rapscallion": "^2.1.14",
    "ratelimiter": "2.0.1",
    "raven": "2.2.1",
    "raw-body": "2.1.4",
    "raw-loader": "0.5.1",
    "react": "^15.6.1",
    "react-addons-linked-state-mixin": "15.6.0",
    "react-addons-pure-render-mixin": "15.6.0",
    "react-autosuggest": "^9.3.4",
    "react-bootstrap": "0.31.0",
    "react-clipboard.js": "^1.1.3",
    "react-countup": "^3.0.3",
    "react-distpicker": "^1.0.1",
    "react-dom": "15.6.1",
    "react-fastclick": "^3.0.2",
    "react-fullpage": "^0.1.18",
    "react-helmet": "2.1.1",
    "react-highcharts": "^16.0.2",
    "react-id-swiper": "^1.6.8",
    "react-infinite": "0.11.0",
    "react-infinite-scroller": "1.0.13",
    "react-iscroll": "2.0.3",
    "react-lazyload": "^2.3.0",
    "react-motion": "0.5.0",
    "react-radio-group": "^3.0.3",
    "react-scroll": "^1.7.10",
    "react-side-effect": "1.0.2",
    "react-slick": "^0.22.3",
    "react-sticky": "5.0.0",
    "react-swipe": "3.0.0",
    "react-swipeable-views": "^0.12.4",
    "react-swipeable-views-core": "^0.12.11",
    "react-swipeable-views-utils": "^0.12.13",
    "react-tap-event-plugin": "^2.0.1",
    "react-tappable": "^1.0.4",
    "react-timer-mixin": "0.13.3",
    "react-transition-group": "^2.3.1",
    "redis": "2.7.1",
    "request": "2.65.0",
    "retry": "0.8.0",
    "rmc-picker": "^5.0.5",
    "rmc-pull-to-refresh": "^1.0.11",
    "routes": "2.1.0",
    "save": "^2.3.2",
    "semver": "5.0.3",
    "simditor": "2.3.6",
    "sphinxapi": "1.1.1",
    "style-loader": "0.13.1",
    "styled-components": "^3.4.10",
    "superagent": "3.5.2",
    "swig": "1.4.2",
    "swipe-js-iso": "2.0.1",
    "tiny-cookie": "0.5.4",
    "truncate": "2.0.0",
    "underscore": "1.8.3",
    "underscore.string": "3.2.2",
    "unicode": "^0.6.1",
    "url-loader": "^1.1.2",
    "urlsafe-base64": "1.0.0",
    "uuid": "2.0.1",
    "vinyl-source-stream": "1.1.0",
    "watchify": "3.6.1",
    "webpack-stream": "3.2.0",
    "weixin-js-sdk": "1.2.0",
    "weixin-pay": "1.1.6",
    "whatwg-fetch": "0.10.0",
    "zepto-webpack": "1.2.1"
  },
  "devDependencies": {
    "aliasify": "2.0.0",
    "babel-core": "6.26.0",
    "babel-loader": "7.1.2",
    "babel-preset-env": "1.6.1",
    "colors": "^1.2.1",
    "css-loader": "0.28.4",
    "eslint": "^4.13.0",
    "eslint-plugin-react": "^7.5.1",
    "gulp-imagemin": "^4.1.0",
    "gulp-plumber": "^1.2.0",
    "happypack": "^4.0.0",
    "hard-source-webpack-plugin": "0.11.1",
    "husky": "^0.14.3",
    "less": "^2.5.3",
    "less-loader": "^4.1.0",
    "react-tappable": "^1.0.4",
    "stylelint": "^9.9.0",
    "webpack": "3.10.0",
    "webpack-bundle-analyzer": "2.8.2",
    "webpack-livereload-plugin": "^1.2.0",
    "webpack-merge": "4.1.1"
  }
}
package.json

啓動:npm start    就是執行   DEBUG='app,body,rpc' gulp watch

重啓:npm restart

中止:npm stop

有時候採用上面的方式啓動node服務時,會報out of memory的錯。那麼咱們能夠用下面的命令。

npm run dev:allocatememory  就如package.json所寫,最大化內存。

 

咱們還常常看見cnpm,這個是淘寶鏡像。

因爲國內連npm服務是比較慢的,因此淘寶就本身出了一個鏡像,同步頻率是10分鐘一次,保證package跟npm一致。

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

安裝後就能夠cnpm代替npm了。好比:cnpm install <module name>

pm2是什麼

 PM2(process manager)是node進程管理工具,能夠利用它來簡化不少node應用管理的繁瑣任務,如性能監控、自動重啓、負載均衡等

 

常見命令:

$ npm install pm2 -g # 命令行安裝 pm2

 

$ pm2 start app.js -i 4 #後臺運行pm2,啓動4個app.js

 

 

# 也能夠把'max' 參數傳遞給 start

 

 

# 正確的進程數目依賴於Cpu的核心數目

 

 

$ pm2 start app.js --name my-api # 命名進程

 

 

$ pm2 list # 顯示全部進程狀態

 

 

$ pm2 monit # 監視全部進程

 

 

$ pm2 logs # 顯示全部進程日誌

 

 

$ pm2 stop all # 中止全部進程

 

 

$ pm2 restart all # 重啓全部進程

 

 

$ pm2 reload all # 0秒停機重載進程 (用於 NETWORKED 進程)

 

 

$ pm2 stop 0 # 中止指定的進程

 

 

$ pm2 restart 0 # 重啓指定的進程

 

 

$ pm2 startup # 產生 init 腳本 保持進程活着

 

 

$ pm2 web # 運行健壯的 computer API endpoint (http://localhost:9615)

 

 

$ pm2 delete 0 # 殺死指定的進程

 

 

$ pm2 delete all # 殺死所有進程

 

 

運行進程的不一樣方式:

 

 

$ pm2 start app.js -i max # 根據有效CPU數目啓動最大進程數目

 

 

$ pm2 start app.js -i 3 # 啓動3個進程

 

 

$ pm2 start app.js -x #用fork模式啓動 app.js 而不是使用 cluster

 

 

$ pm2 start app.js -x -- -a 23 # 用fork模式啓動 app.js 而且傳遞參數 (-a 23)

 

 

$ pm2 start app.js --name serverone # 啓動一個進程並把它命名爲 serverone

 

 

$ pm2 stop serverone # 中止 serverone 進程

 

 

$ pm2 start app.json # 啓動進程, 在 app.json裏設置選項

 

 

$ pm2 start app.js -i max -- -a 23 #在--以後給 app.js 傳遞參數

 

 

$ pm2 start app.js -i max -e err.log -o out.log # 啓動 並 生成一個配置文件

 

 

你也能夠執行用其餘語言編寫的app ( fork 模式):

 

 

$ pm2 start my-bash-script.sh -x --interpreter bash

 

 

$ pm2 start my-python-script.py -x --interpreter python
相關文章
相關標籤/搜索