简体   繁体   English

如何将 React + Node.js 应用程序部署到 Heroku。 收到来自反应的 axios 请求的错误 405

[英]How to deploy React + Node.js application to Heroku. Getting error 405 on axios request from react

can you please look into my code?你能看看我的代码吗? I am able to create an app on localhost:5000 (node+react) but when I deployed it in Heroku I can't get the response from the POST request.我能够在 localhost:5000 (node+react) 上创建一个应用程序,但是当我在 Heroku 中部署它时,我无法从 POST 请求中获得响应。 All it is giving me is 405 error.它给我的只是405错误。 I have been fixing it for 2 days but not getting any solution.我已经修复了 2 天,但没有得到任何解决方案。 If you can help me it will be highly appreciated.如果您能帮助我,我们将不胜感激。

Here is the error message from web console: Uncaught (in promise) Error: Request failed with status code 405这是来自 web 控制台的错误消息:未捕获(承诺中)错误:请求失败,状态码 405

here is my github repo link to review my code: github.com/sajpanchal/portfolio这是我的 github 回购链接以查看我的代码:github.com/sajpanchal/portfolio

Here is my react code aboutme.js:这是我关于me.js的反应代码:

    handleSubmit = (event) => {
        const data = { ...this.state.data };
        event.preventDefault();
        axios.post(`${BASE_API_URL}/api/email`, data).then((res) => {
          console.log(res);
          console.log(res.data);
        });
      };

Here is my node.js file index.js:这是我的 node.js 文件 index.js:

const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const logger = require("morgan");

const app = express();
var nodemailer = require("nodemailer");
require("dotenv").config();

const { USER_PASSWORD, USER_EMAIL, RECEIVER_EMAIL } = process.env;
console.log(USER_PASSWORD);
//React build app setup
app.use(express.static(path.join(__dirname, "build"))); // serve all static files from build

app.use(logger("dev"));

app.get("/", (req, res) => {
  res.sendFile(path.join(__dirname, "build", "index.html")); // this will keep our client side routing functional.
});

app.use(bodyParser.json());
// // allows your app to interact with the apps running on different servers.

//this will set the htstp server response header.
app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PUT, PATCH, DELETE"
  );
  res.setHeader("Acess-Control-Allow-Headers", "Content-Type , Authorization");
  next();
});
//get request
app.get("/api", (req, res, next) => {
  res.send("API status: OK");
});

var transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: USER_EMAIL,
    pass: USER_PASSWORD,
  },
});

app.post("/api/email", (req, res, next) => {
  var mailOptions = {
    from: req.body.email,
    to: RECEIVER_EMAIL,
    subject: req.body.subject,
    html: `<strong>sender: ${req.body.email}</strong><br>Message: <strong>${req.body.message}</strong>`,
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      res.status(400).json({
        error: error,
      });
    } else {
      res.status(200).json({
        success: true,
      });
    }
  });
});

const port = process.env.PORT || 5000;
app.listen(port, "0.0.0.0");
console.log("connected to port: ", port);

the full project is available on github: https://github.com/sajpanchal/portfolio完整项目可在 github 上获得: https://github.com/sajpanchal/portfolio

Please note that I have added nodejs project into my react project root folder and then I created a build version of react for node project.请注意,我已将 nodejs 项目添加到我的 react 项目根文件夹中,然后我为 node 项目创建了 react 的构建版本。

Here is the error screenshot:这是错误截图: 在此处输入图像描述

Heroku log: Heroku 日志: 在此处输入图像描述

Please disregard the above question as I found the solution.请忽略上述问题,因为我找到了解决方案。

So, I literally did 2 things:所以,我确实做了两件事:

1> I reinstalled the node packages in both node and react apps. 1> 我在节点和反应应用程序中重新安装了节点包。 I can possibly install new packages that are required in your projects and we can get rid of unused ones.我可能会安装您项目中所需的新软件包,我们可以摆脱未使用的软件包。

2> There was some issue with my Heroku App. 2> 我的 Heroku 应用程序存在一些问题。 So I deleted it and created a new one.所以我删除了它并创建了一个新的。

Then I deployed my updated app from git to the new Heroku app and it started to work as expected!然后我将更新的应用程序从 git 部署到新的 Heroku 应用程序,它开始按预期工作!

Thanks to all who contributed to this post.感谢所有为这篇文章做出贡献的人。 Cheers.干杯。

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

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