我制作了一个React应用程序,它与我的API服务器托管在本教程指导下的同一台机器上。
我的目录是这样设置的
- root
- server.js (entry for the API server)
- app (for all back-end stuff)
- client (directory for React app)
- public (static files used in the front-end)
- src (for all front-end stuff)
- build (directory containing static build)
- index.html (entry for front-end)
- ... (other bundled stuff)在下面的方法中,我提供了我的React应用程序的静态副本。
// server.js
const app = express();
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.use('/api', routes); // for API routes我遇到了一个问题,当我在本地启动它时,(用NODE_ENV=production)所有的东西都平滑,页面刷新不会破坏应用程序。
但是,在我将其部署到ElasticBean秸秆之后,页面刷新会破坏应用程序,在浏览器中显示此html错误消息。
Cannot GET /{whichever url path I had prior to refresh}
我可以从日志中看到浏览器试图用GET /{url_pathname}向服务器发送请求
我最初怀疑有反应路由器和快速路由器的有趣之处,但又一次,我感到困惑,为什么这与本地主机的情况不一致。
发布于 2018-02-12 19:20:48
如果您正在运行一个基本上在单个索引文件上运行的react应用程序,那么您的中间件路由器应该有一个始终指向某个索引文件的捕获路由。
app.get('/*',(req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})请注意,当前的示例依赖于path module...So:
import path from 'path'
// or
const path = require('path')发布于 2018-02-12 19:32:41
如果使用的是NGINX,则需要更新位于/etc/nginx/sites-available的配置文件。
location / {
try_files $uri $uri/ /index.html$is_args$args;
}或者,如果您的应用程序不在默认目录中,例如" app -location“,您就会得到这个。
location /app-location {
root /var/www;
index index.html;
try_files $uri $uri/ /app-location/index.html;
}并重新启动nginx
sudo service nginx restarthttps://stackoverflow.com/questions/48753760
复制相似问题