简体   繁体   English

部署在 Digital Ocean 上的 React App 出现 404 错误

[英]React App deployed on Digital ocean gives 404 Error

I have a validation form on React but on clicking submit m getting "404 Error".我有一个关于 React 的验证表单,但是在单击提交时出现“404 错误”。 It works perfect on local machine.它在本地机器上完美运行。 //from app.js //来自app.js

handleSubmit = async e => {
    e.preventDefault();
    const response = await fetch('/api/emailVerifier', {
      method: 'POST',
      body: this.state,
      headers: {
        'Content-Type': 'application/json',
      },

      //body: JSON.stringify({ post: this.state.post }),

    });
    const body = await response.text();
    
    this.setState({ responseToPost: body });
  };

//from server.js //来自 server.js

app.post('/api/emailVerifier', async (req, res) => {
  console.log(req.body);
  const emailValidator = new EmailValidator();
  const { wellFormed, validDomain, validMailbox } = await emailValidator.verify(req.body.post);
  res.send(

    `response received , welllformed = ${wellFormed}, validDomain = ${validDomain}, validMailbox = ${validMailbox}`,
  );
});

i have installed my my validator on server using npm i add deep-email-validator --save我已经使用 npm 在服务器上安装了我的验证器我添加了 deep-email-validator --save

My Directory on server has我的服务器上的目录有

asset-manifest.json  index.html   manifest.json      package.json  static
build                logo192.png  node_modules       robots.txt
favicon.ico          logo512.png  package-lock.json  server.js

Can you access other endpoints in server.js on DigitalOcean?您可以访问 DigitalOcean 上 server.js 中的其他端点吗? EG: if you create:例如:如果您创建:

app.get('/', function (req, res) {
  res.send('hello world')
})

And make a request to it from your React app does it reach that endpoint?并从您的 React 应用程序向它发出请求,它会到达那个端点吗?


I think you will struggle to run the React app and server from the same location.我认为您将很难从同一位置运行 React 应用程序和服务器。

If your React app is setup correctly it will try to handle all requests coming to that url.如果您的 React 应用程序设置正确,它将尝试处理来自该 url 的所有请求。

You could run the express server on a different port to the React app.你可以在与 React 应用程序不同的端口上运行 express 服务器。 Then traffic won't get routed to both the React server and your Express server.然后流量不会同时路由到 React 服务器和您的 Express 服务器。

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

相关问题 部署在生产环境(Digital Ocean)上的 React App 未在浏览器上加载 - React App deployed on production (Digital Ocean) is not loading on browser 在数字海洋上的反应应用程序中上传图像 - Uploading image in react app on digital ocean 如何在数字海洋上部署create react应用程序? - How to deploy create react app on digital ocean? 如何在数字海洋上部署 React 应用程序? - How to deploy a react app on digital ocean? 使用 dokku 在 Digital Ocean 上创建反应应用程序部署失败 - Create-react-app deployment failure on Digital Ocean using dokku React 应用程序在部署到 GCP 时出现 404 错误 - React app giving 404 error when deployed to GCP 部署的 React 应用程序显示一个空白页面并且控制台返回错误 404 - Deployed React App Shows a Blank Page and Console Returns Error 404 React Router 在 Digital Ocean 上失败 - React Router fails on Digital Ocean Passportjs Google OAuth20 在数字海洋上部署时不起作用 - Passportjs Google OAuth20 not working when deployed on digital ocean 如何通过Passenger / Nginx在数字海洋上部署Django / React / Webpack应用 - How to deploy Django/React/Webpack app on Digital Ocean through Passenger/Nginx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM