[英]CORS not working on Heroku but works locally with Node js
I have a contact form on my Angular Js website with a back end of express and node mailer for emails which works fine when i test it locally but when i deploy it on Heroku i get an error: CORS request did not succeed.我的 Angular Js 网站上有一个联系表单,带有用于电子邮件的 express 和节点邮件程序的后端,当我在本地测试时可以正常工作,但是当我在 Heroku 上部署它时出现错误:CORS 请求没有成功。 I've been on this for a while without a way, any suggestion would be great.Thanks
我已经有一段时间没有办法了,任何建议都会很棒。谢谢
here is my code这是我的代码
const cors = require("cors");
const http = require("http");
const path = require('path');
const nodemailer = require("nodemailer");
var bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(cors({origin:"*"}));
const port = process.env.PORT || 1996
app.use(express.static(__dirname + "/dist/Orisha"));
app.get('/*', (req,res)=>res.sendFile(path.join(__dirname+"/dist/Orisha/index.html")));
app.post("/sendmail", (req,res)=>{
console.log(req.body, 'data of form');
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
secure: 'true',
port: '465',
auth: {
user: 'hhdhjssw@gmail.com', // must be Gmail
pass: 'password'
}
});
var mailOptions = {
from: `${req.body.email}`,
to: 'hdsywuxwjd@gmail.com', // must be Gmail
cc:`${req.body.firstName} <${req.body.email}>`,
subject: `${req.body.subject}`,
html: `
<table style="width: 100%; border: none">
<thead>
<tr style="background-color: #000; color: #fff;">
<th style="padding: 10px 0">Name</th>
<th style="padding: 10px 0">E-mail</th>
<th style="padding: 10px 0">Message</th>
<th style="padding: 10px 0">Feedback</th>
</tr>
</thead>
<tbody>
<tr>
<th style="text-align: center">${req.body.firstName} ${req.body.lastName}</th>
<td style="text-align: center">${req.body.lastName}</td>
<td style="text-align: center">${req.body.Email}</td>
<td style="text-align: center">${req.body.message}</td>
<td style="text-align: center">${req.body.feedback}</td>
</tr>
</tbody>
</table>
`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
res.status(200).json({
message: 'successfuly sent!'
})
}
});
})
const server = http.createServer(app);
server.listen(port,()=>console.log("Running on http://localhost:1996/"))
And this is the error i get on Heroku:这是我在 Heroku 上遇到的错误:
That's because your on https trying to access a resource that is actually running over http.那是因为您在 https 上尝试访问实际通过 http 运行的资源。 Same-origin Policy
同源策略
You can do two things to solve this problem:你可以做两件事来解决这个问题:
http://orishaa.herokuapp.com
http://orishaa.herokuapp.com
: http://orishaa.herokuapp.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.