npm,pm2等相关知识的学习

现在开始接手node端测试,有好多知识点,比如启动进程的命令,查看进程的命令都不是很清晰,现在具体来学习下~

npm由来

前端最大的社区是GitHub,大家在这里分享代码,讨论问题,收集学习资源。大家需要依赖什么安装包从以前的去官网下载到去GitHub下载,还是很麻烦。这时候一个拥有三大美德的程序员 Isaac Z. Schlueter (以下简称 Isaaz)给出一个解决方案:用一个工具把这些代码集中到一起来管理吧!

这个工具就是他用 JavaScript (运行在 Node.js 上)写的 npm,全称是 Node Package Manager

NPM 的实现思路大概是这样的:

  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包管理器。

npm的应用

新版的nodejs 已经集成了npm,所以安装nodejs的时候,npm也一并安装好了。

常用命令:

安装模块:npm install <module name>

卸载模块:npm uninstall <module name>

更新模块:npm update <module name>

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

 

转载于:https://www.cnblogs.com/lixiaoxuan/p/10296650.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值