简体   繁体   English

使用 PM2 永远运行 NodeJs http-server

[英]Running NodeJs http-server forever with PM2

My question is about running HTTP-server in combination with PM2.我的问题是关于结合 PM2 运行 HTTP 服务器。

The problem I face is that:我面临的问题是:

  1. HTTP-server requires as input a folder which is the root of the website and a port number to run the website on. HTTP-server 需要一个文件夹作为输入,该文件夹是网站的根目录和运行网站的端口号。
  2. PM2 doesn't recognize the HTTP-server command, even when HTTP-server is installed with the -g option.即使使用-g选项安装了 HTTP-server,PM2 也无法识别 HTTP-server 命令。

So I tried the following (note the double dash which should pass the parameters to the HTTP-server script:所以我尝试了以下操作(注意应该将参数传递给 HTTP 服务器脚本的双破折号:

/node_modules/http-server/lib$ pm2 start http-server.js -- /home/unixuser/websiteroot -p8686

But it doesn't work.但它不起作用。

I also tried:我也试过:

http-server /home/unixuser/websiteroot -p8686

Which does work, but doesn't have the great support of pm2 ?哪个有效,但没有 pm2 的大力支持?

Any suggestions would be great, thanks!任何建议都会很棒,谢谢!

You almost had it.你几乎拥有它。

Check where http-server is located by executing:通过执行以下命令检查 http-server 所在的位置:

$ which http-server

You should get something like this /usr/bin/http-server你应该得到这样的东西/usr/bin/http-server

Then cd to the directory you want to serve files from and execute:然后cd到您要从中提供文件的目录并执行:

$ pm2 start /usr/bin/http-server --name my-file-server -- -p 8080 -d false

--name my-file-server is optional, but -- is required to pass arguments through to the http-server command. --name my-file-server是可选的,但是--需要通过对来传递参数http-server命令。

pm2 start <location>/http-server --name http-server -- -p <port> -d false

or或者

PM2 modules it self has in-build static file to be served, which is similar to http-server https://pm2.keymetrics.io/docs/usage/expose/ PM2 模块本身具有要提供的内置静态文件,类似于 http-server https://pm2.keymetrics.io/docs/usage/expose/

pm2 serve <path> <port>
pm2 start 'http-server-spa websiteroot index.html 8080'

if we have a build generated by grunt,then go to its path and hit:如果我们有一个由 grunt 生成的构建,那么转到它的路径并点击:

~/app/build/prod$ sudo pm2 start /usr/local/bin/http-server -p 8080

Now check app status at localhost:8080现在在localhost:8080检查应用程序状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM