Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I serve static files with 404 redirection by PM2?

Tags:

pm2

I already know that there is a simple command to serve static folder by PM2:

pm2 serve <path> <port>

But how can I add 404 redirection to it? e.g. redirect to 404.html while that happends. Can't find that on Google & PM2 doc.

like image 928
wxsm Avatar asked Nov 28 '17 06:11

wxsm


1 Answers

By default, pm2 displays 404.html from the serving directory if it can't map a request to any static file of that directory. For example, assume that your static file directory name is foo, which contains three images - 1.jpg, 2.jpg, 3.jpg. You are serving those files using:

pm2 serve <path_of_foo> 8080

Now, if the server receives any request like http://localhost:8080/bar.jpg, pm2 will look for a file named 404.html on foo directory and display its contents, as bar.jpg does not exist. If it doesn't find any, it show a simple text message 404 Not Found. There is no options to make it configurable (CLI or JSON), as per my knowledge.

However, if you make this file path configurable, you can take a look at here and customize the source according to your need.

like image 151
SALEH Avatar answered Oct 12 '22 09:10

SALEH