xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

PM2 & nodemon & Node.js Deamon All In One

PM2 & nodemon & Node.js Deamon All In One

node.js server tools

PM2

https://pm2.keymetrics.io/

https://pm2.io/

$ yarn global add  pm2
# OR
$ npm install pm2 -g

# global
$ pm2 start server/index.js
$ yarn add -D pm2
# OR
$ npm i -D pm2

# local
$ ./node_modules/pm2/bin/pm2 start server/index.js

$ ./node_modules/pm2/bin/pm2 list

# $ ./node_modules/pm2/bin/pm2 stop ID
$ ./node_modules/pm2/bin/pm2 stop 0

$ ./node_modules/pm2/bin/pm2 monit

Express & PM2

https://expressjs.com/en/advanced/pm.html

https://pm2.keymetrics.io/docs/usage/quick-start/

$ pm2 start app.js

$ pm2 monit

$ pm2 list

$ pm2 stop

$ pm2 restart

$ pm2 delete

$  pm2 plus

PM2 ecosystem.config.js

ecosystem.config.js

module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }]
}

# $ pm2 [start|restart|stop|delete] ecosystem.config.js

$ pm2 start ecosystem.config.js

https://doc.pm2.io/en/runtime/guide/ecosystem-file/

https://pm2.keymetrics.io/docs/usage/application-declaration/

nodemon

Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.

Nodemon是一个实用程序,它将监视源中的任何更改并自动重新启动服务器。

https://nodemon.io/

$ yarn global add  nodemon
# OR
$ npm i -g nodemon

# global
$ nodemon server/index.js
$ yarn add -D nodemon
# OR
$ npm i -D nodemon

# local
$ ./node_modules/nodemon/bin/nodemon.js ./server.js localhost 8080

$ ./node_modules/nodemon/bin/nodemon.js --inspect ./server.js 80

# js !== cli 👎❌
# $ ./node_modules/nodemon/bin/nodemon ./src/index.js
# ✅
$ ./node_modules/nodemon/bin/nodemon.js ./src/index.js

nodemon.json

{
  "verbose": true,
  "ignore": ["*.test.js", "fixtures/*"],
  "execMap": {
    "rb": "ruby",
    "pde": "processing --sketch={{pwd}} --run"
  }
}

package.json

{
  "name": "nodemon",
  "homepage": "http://nodemon.io",
  "...": "... other standard package.json values",
  "nodemonConfig": {
    "ignore": ["test/*", "docs/*"],
    "delay": "2500"
  }
}

TypeScript

lcoal nodemon & node ✅

{
  "scripts": {
    "dev": "ts-node ./src/app.ts",
    "build": "tsc -w ./src/crawler.ts",
    "build-all": "tsc -w",
-   "auto": "./node_modules/nodemon/bin/nodemon.js ./dist/app.js",
+   "auto": "nodemon node ./dist/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
}

npx

PM2

# global
$ pm2 start server/index.js

# local
$ ./node_modules/pm2/bin/pm2 start server/index.js

# npx 等价于 ./node_modules/pm2/bin/ ✅
$ npx pm2 start server/index.js


nodemon

# global
$ nodemon ./server.js

# local
$ ./node_modules/nodemon/bin/nodemon.js ./server.js


# npx 等价于 ./node_modules/nodemon/bin/ ✅
$ npx nodemon server.js

https://www.ruanyifeng.com/blog/2019/02/npx.html

daemon 守护进程

https://en.wikipedia.org/wiki/Daemon_(computing)

https://translate.google.com/?sl=en&tl=zh-CN&text=daemon&op=translate

refs

https://www.npmjs.com/package/nodemon
https://github.com/remy/nodemon

https://www.npmjs.com/package/pm2
https://github.com/Unitech/pm2



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-09-06 11:18  xgqfrms  阅读(508)  评论(10编辑  收藏  举报