【问题标题】:React App deployed on Digital ocean gives 404 Error部署在 Digital Ocean 上的 React App 出现 404 错误
【发布时间】:2020-12-01 11:22:13
【问题描述】:

我在 React 上有一个验证表单,但是在单击提交时出现“404 错误”。 它在本地机器上完美运行。 //来自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 });
  };

//来自 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}`,
  );
});

我已经使用 npm 在服务器上安装了我的验证器我添加了 deep-email-validator --save

我在服务器上的目录有

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

【问题讨论】:

  • 在不了解更多上下文(即客户端和服务器结构、构建和发布脚本等)的情况下很难回答您的问题。但是很明显,一旦发布到您的云,您实现 /api/emailVerifier 的 Web 服务器就会丢失提供者。
  • 请检查 .. 我已经更新了答案
  • 可能不是您正在寻找的,但digitalocean.com/community/tutorials/… 可能会有所帮助。

标签: reactjs digital-ocean


【解决方案1】:

您可以访问 DigitalOcean 上 server.js 中的其他端点吗? EG:如果你创建:

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

然后从您的 React 应用程序向它发出请求,它会到达那个端点吗?


我认为您将很难从同一位置运行 React 应用程序和服务器。

如果您的 React 应用程序设置正确,它将尝试处理所有到达该 url 的请求。

您可以在与 React 应用程序不同的端口上运行 express 服务器。然后流量将不会同时路由到 React 服务器和您的 Express 服务器。

【讨论】:

  • 你能详细说明一下吗? m 无法理解,因为 m 是响应开发的新手
  • @allabtcoding 我已经用更多信息更新了答案
  • 不,我试过了..作为 web 开发的新手,你能不能更详细地指导一下
  • app.get('/api/hello', (req, res) => { res.send({ express: 'Enter Email Address to' }); });
  • @allabtcoding 在不同的端口上运行 express app.listen(4000); .. 然后向 app.post(':4000/api/emailVerifier' 提出请求
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2017-07-13
  • 2020-09-02
  • 1970-01-01
  • 2019-01-20
  • 2017-01-25
相关资源
最近更新 更多