Skip to content

Nodejs教程25:启动器 #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chencl1986 opened this issue Mar 21, 2019 · 0 comments
Open

Nodejs教程25:启动器 #56

chencl1986 opened this issue Mar 21, 2019 · 0 comments

Comments

@chencl1986
Copy link
Owner

阅读更多系列文章请访问我的GitHub博客,示例代码请访问这里

为何要使用启动器

常用的启动器有forever、pm2等,它们主要用在项目部署阶段

  1. 使应用不间断运行,如果不使用启动器,命令行窗口一旦关闭,或者出现报错,应用就会停止运行,启动器会帮助应用自动重启。
  2. 若出现服务器重启,启动器会自动启动应用,不需要手动操作。

常用的启动器有forever、pm2等,接下来介绍一下forever的使用。

forever

forever文档可参考:https://github.com/foreverjs/forever#readme

使用forever启动一个服务:

  1. 安装forever:npm install forever -g
  2. 在命令行运行forever start server.js,替代node server.js命令。
  3. 命令行窗口提示info: Forever processing file: server.js,表示启动成功,此时如果将窗口关闭,应用照样可以访问。
  4. 如果需要关闭服务,可以运行forever stop server.js。还有一个命令是forever stopall,停止全部在运行的任务,但使用要慎重。

forever启动时,还可以添加一些配置,例如forever start xxx.js -l c:/xxx.log -e c:/xxx_err.log -a,forever start xxx.js表示启动xxx.js。

-l c:/xxx.log表示将log信息输出到c:/xxx.log文件。

-e c:/xxx_err.log表示将错误信息输出到c:/xxx_err.log文件。

-a表示新的日志添加到旧日志之后,即保留旧日志。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant