首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布http://localhost:3001/send net::ERR_CONNECTION_REFUSED

发布http://localhost:3001/send net::ERR_CONNECTION_REFUSED
EN

Stack Overflow用户
提问于 2021-05-27 04:06:40
回答 1查看 136关注 0票数 0

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

下面是我的提交函数的代码片段。

代码语言:javascript
复制
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}
EN

回答 1

Stack Overflow用户

发布于 2021-05-27 04:32:51

代码语言:javascript
复制
 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上测试它:

代码语言:javascript
复制
 let url;
            process.env.NODE_ENV === 'production' ?  url = `http://localhost:3001/send`
                : url = "http://localhost:3001/send";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67712014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档