【问题标题】:POST http://localhost:3001/send net::ERR_CONNECTION_REFUSEDPOST http://localhost:3001/send net::ERR_CONNECTION_REFUSED
【发布时间】:2021-05-29 03:33:09
【问题描述】:

我在使用 nodemailer 获取联系表以在生产环境中运行时遇到问题。在开发中,我成功发送电子邮件,但在生产中我收到以下错误:“POST http://localhost:3001/send net::ERR_CONNECTION_REFUSED”和“Uncaught (in promise) TypeError: Failed to fetch”。

这是我的提交功能的代码的 sn-p。

const submitEmail = async (e) => {
    e.preventDefault();
    console.log({ formdata });
    let url;
        process.env.NODE_ENV === 'production' ?  url = `https://my-app.herokuapp.com/send`
            : url = "http://localhost:3001/send";
    const response = await fetch(url, {
      method: "POST",
      mode: "cors",
      cache: "no-cache",
      headers: {
        "Content-type": "application/json",
        'Accept': 'application/json'
      },
      body: JSON.stringify({ formdata }),
    })
      .then((res) => res.json())
      .then(async (res) => {
        const resData = await res;
        console.log(resData);
        if (resData.status === "success") {
          setMessage("Message Sent!");
        } else if (resData.status === "fail") {
          setMessage("Message failed to send");
        }
      })
      .then(() => {
        setFormdata({
          name: "",
          email: "",
          subject: "",
          message: "",
        });
      });
  };```

I have attempted to implement the following solutions with no luck:{unsuccessful}
1: Change the url address from localhost, to heroku address.{unsuccessful}
2: Utilize a cors-anywhere proxy (url ="cors-anywhere.herokuapp.com/localhost:3001/send"){unsuccessful}
3: Edit the environment variables in heroku {unsuccessful}
4: Change the port number in server.js {unsuccessful}
5: Added Procfile to manually start node and npm server {unsuccessful}

【问题讨论】:

    标签: node.js reactjs express heroku nodemailer


    【解决方案1】:
     let url;
            process.env.NODE_ENV === 'production' ?  url = `https://.herokuapp.com/send`
                : url = "http://localhost:3001/send";
    

    好吧,你的代码中的这一行说,当 NODE_ENV 将是“生产”时,URL 将

    https://.herokuapp.com/send

    上面的 URL 似乎不是一个有效的 URL,因为 Heroku 在 herokuapp 之前有一个子域,比如 my-app.herokuapp.com

    您可以简单地在 localhost 上执行以下操作来测试它:

     let url;
                process.env.NODE_ENV === 'production' ?  url = `http://localhost:3001/send`
                    : url = "http://localhost:3001/send";
    

    【讨论】:

      猜你喜欢
      • 2019-12-23
      • 2021-03-30
      • 1970-01-01
      • 2021-12-16
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2021-12-12
      相关资源
      最近更新 更多