Running NodeJs http-server forever with PM2
Asked Answered
T

4

21

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!

Togs answered 4/8, 2015 at 9:2 Comment(1)
you can go to your build path and hit:build/prod$ sudo pm2 start /usr/local/bin/http-server -p 8080Trudge
W
51

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.

Waldron answered 28/8, 2015 at 9:22 Comment(3)
You will often have multiple versions/installations of the same executable on the same machine and which is handy to see which one is linked to the command.Waldron
Thanks for mentioning -- to pass arguments to http-server, i came for thatAyeshaayin
Or do in one go pm2 start $(which http-server) --name my-file-server -- -p 8080 -d false. No need to manually which, copy and cd.Worcestershire
D
12
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>
Debbradebby answered 31/10, 2019 at 13:2 Comment(0)
F
3
pm2 start 'http-server-spa websiteroot index.html 8080'
Flexion answered 28/9, 2018 at 20:41 Comment(0)
T
0

if we have a build generated by grunt,then go to its path and hit:

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

Now check app status at localhost:8080

Trudge answered 11/2, 2017 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.