简体   繁体   English

使用React,NodeJS部署问题,在生产中表达

[英]Deploying issue with react,nodejs,express on production

This is my first project using nodejs and react, I have been working a aplication by following this this tutorial .Its working fine localhost 这是我的第一个使用nodejs和react的项目,我一直在按照本教程进行应用。它可以正常运行localhost

but not working on prodution mode.I have created a build and its generated a directory called "dist". 但无法在生产模式下工作。我创建了一个构建,并生成了一个名为“ dist”的目录。 I have moved everthing to live server from "Dist" folder. 我已经将所有内容从“ Dist”文件夹移至实时服务器。

But the node route not working , its says 404 error.How to deploy nodejs with react on production? 但是节点路由不起作用,它显示404错误。如何在生产中响应部署nodejs?

Can please help me get rid of it? 能帮我摆脱它吗?

Thanks 谢谢

Your backend server will not know how to handle the routes on your client or know the location of your "client" folder, if you have a dist folder you would need to do something similar to this: 您的后端服务器将不知道如何处理客户端上的路由,也不知道“客户端”文件夹的位置,如果您有dist文件夹,则需要执行以下操作:

if (process.env.NODE_ENV === 'production') {
  app.use(express.static('dist'));
  const path = require('path');
  app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'dist', 'index.html'));
 });
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM