Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running NodeJs http-server forever with PM2

My question is about running HTTP-server in combination with PM2.

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.
  2. PM2 doesn't recognize the HTTP-server command, even when HTTP-server is installed with the -g option.

So I tried the following (note the double dash which should pass the parameters to the HTTP-server script:

/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 ?

Any suggestions would be great, thanks!

like image 719
Guy Hagemans Avatar asked Aug 04 '15 09:08

Guy Hagemans


People also ask

Does PM2 run forever?

PM2. PM2 is a production process manager for Node. js applications, that has a built-in load balancer. PM2 allows you to keep applications alive forever and reload them without downtime, and will facilitate common system admin tasks.

Which is better forever or PM2?

According to the StackShare community, PM2 has a broader approval, being mentioned in 74 company stacks & 107 developers stacks; compared to forever, which is listed in 3 company stacks and 3 developer stacks.


2 Answers

You almost had it.

Check where http-server is located by executing:

$ which http-server

You should get something like this /usr/bin/http-server

Then cd to the directory you want to serve files from and execute:

$ 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.

like image 60
gwest7 Avatar answered Sep 21 '22 00:09

gwest7


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 serve <path> <port>
like image 32
Madan Avatar answered Sep 23 '22 00:09

Madan