react使用create-react-app创建的项目部署

一、在所有的项目代码编写完成后,react项目直接部署是无法正常访问的

    1、问题一

          网页无法正常的浏览器刷新,刷新会报404错,路由找不到页面

    2、问题二

          路由跳转后,浏览器后退按钮点击后,页面不刷新

    3、问题三

          使用HashRouter的react项目会在路由的时候在路径上添加/#,所以尽量使用BrowserRouter。

二、正确部署

     1、在public文件夹下添加.htaccess文件,用记事本或编辑器打开,将下列代码拷贝到文件中,并保存

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.(css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . index.html [L]

 2、在react项目的根目录使用  npm run-script build打包文件,显示如下信息为打包成功

Search for the keywords to learn more about each warning.
To ignore, add // eslint-disable-next-line to the line before.

File sizes after gzip:

  118.12 KB  buildstaticjsmain.781fed84.js
  60.21 KB   buildstaticcssmain.4ef2f659.css

The project was built assuming it is hosted at the server root.
You can control this with the homepage field in your package.json.
For example, add this to build it for GitHub Pages:

  "homepage" : "http://myname.github.io/myapp",

The build folder is ready to be deployed.
You may serve it with a static server:

  yarn global add serve
  serve -s build

Find out more about deployment here:

  http://bit.ly/2vY88Kr

  3、最后将打包后生成的build文件部署到服务器上

参考链接:链接一,react项目部署

                  链接二,react项目部署

原文地址:https://www.cnblogs.com/detanx/p/reactUpServer.html