我在使用nodemailer在生产环境中使用联系人表单时遇到了问题。在开发中,我成功地发送了电子邮件,但是在生产中,我收到了以下错误:"POST http://localhost:3001/send net::ERR_CONNECTION_REFUSED",和"Uncaught (in promise) TypeError: Failed to fetch“。
下面是我的提交函数的代码片段。
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}发布于 2021-05-27 04:32:51
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";https://stackoverflow.com/questions/67712014
复制相似问题