1

I have an reactjs application that I have deploy on my web server.

I build my app (So it's create a build folder) and when I want to start it with ~/.npm-packages/bin/serve -s ./build -l tcp://0.0.0.0:8100 it work (with work dir set to react/, build is here react/build but we can see all source code on my page, that theory not possible in production mode.

If I set the work dir to react/build it return a 404 error.

Inside build folder

Inside build

It doesn't return any other error unfortunately.

Update: I only stock build folder and it work, but we can always see source code even its in production mode.

If I change to react/build, react/build/static or other, it display 404.

Tryliom
  • 895
  • 1
  • 12
  • 37
  • 1
    Since you are trying to deploy on production,I would rather suggest using `pm2` along with `serve` to fulfill your purpose. You can find a details about how to deploy using pm2 here in the link https://www.loginradius.com/engineering/blog/react-app-deployment/ – Aman Aug 21 '20 at 12:04

1 Answers1

3

If you are using npm's serve (the default for create-react-app, which I am assuming you are using), that second argument is the directory to serve:

$ serve --help

$ serve} [-l listen_uri [-l ...]] [directory]

By default, serve will listen on 0.0.0.0:5000 and serve the
current working directory on that address.

-s, --single                        Rewrite all not-found requests to `index.html`

But serve is meant for development serving. It works fine for production of a small static site, but consider another production-proven web server.

Your unminified source should not be kept on a production server, at all. You should deploy just the build files to production.

Nick
  • 4,901
  • 40
  • 61
  • Finally I have remove the option `-s ./build` and it work – Tryliom Jun 27 '18 at 14:48
  • 1
    `-s` and `./build` are two different options, not one: `-s` is to redirect 404s to an index.html file, and the directory is the base path to serve. Without the optional directory, it serves the present working directory -- which appears to be the problem you had. – Nick Jun 27 '18 at 14:50