【发布时间】: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