Javascript “我如何更正?”;否';访问控制允许原点';标题出现在请求的资源上;Firebase函数在express中出错?
我正在尝试通过具有Firebase功能的NodeEmailer发送邮件。邮件的数据将从表单中提供,但我得到了这个错误 从源“my Angular web app”访问“my firebase functions”处的XMLHttpRequest已被CORS策略阻止:请求的资源上不存在“Access Control Allow origin”标头 这是我的Firebase函数Javascript “我如何更正?”;否';访问控制允许原点';标题出现在请求的资源上;Firebase函数在express中出错?,javascript,firebase,express,google-cloud-functions,cors,Javascript,Firebase,Express,Google Cloud Functions,Cors,我正在尝试通过具有Firebase功能的NodeEmailer发送邮件。邮件的数据将从表单中提供,但我得到了这个错误 从源“my Angular web app”访问“my firebase functions”处的XMLHttpRequest已被CORS策略阻止:请求的资源上不存在“Access Control Allow origin”标头 这是我的Firebase函数 app.use((req, res, next) => { // Website you wish to
app.use((req, res, next) => {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'https://stanleyogbonna-5228d.web.app');
res.setHeader('Access-Control-Allow-Origin', 'https://stanleyogbonna-5228d.firebaseapp.com');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Content-Type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
//to make it work you need gmail account
const gmailEmail = functions.config().gmail.login;
const gmailPassword = functions.config().gmail.pass;
admin.initializeApp();
app.post('/visitor/contact', (req, res, next) => {
//transporter is a way to send your emails
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword
}
});
const mailOptions = {
from: gmailEmail,
to: req.body.email,
subject: 'Full stack developer',
html: `<h1 style="color:blue;">Welcome ${req.body.firstName}</h1>
<p style="font-size:25px;">Thank you very much for contacting me. i hope you are
having a great time where ever you may be.</p>
<p style="font-size:25px;">I am a full stack developer by training and i am available at the moment for a MEAN stack job That will challenge me to be better.</p>
<p style="font-size:23px;">Thanks ${req.body.firstName}</p>`
};
transporter.sendMail(mailOptions);
})
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
exports.app = functions.https.onRequest(app);
app.use((请求、恢复、下一步)=>{
//您希望允许连接的网站
res.setHeader('Access-Control-Allow-Origin','https://stanleyogbonna-5228d.web.app');
res.setHeader('Access-Control-Allow-Origin','https://stanleyogbonna-5228d.firebaseapp.com');
//请求您希望允许的方法
res.setHeader('Access-Control-Allow-Methods'、'GET、POST、OPTIONS、PUT、PATCH、DELETE');
//您希望允许的请求头
res.setHeader('Access-Control-Allow-Headers','X-request-With,content-type,content-type');
//如果您需要网站在发送的请求中包含cookie,请设置为true
//到API(例如,如果您使用会话)
res.setHeader('Access-Control-Allow-Credentials',true);
//传递到中间件的下一层
next();
});
//要让它工作,你需要gmail帐户
const gmailEmail=functions.config().gmail.login;
const gmailPassword=functions.config().gmail.pass;
admin.initializeApp();
app.post('/visitor/contact',(请求、回复、下一步)=>{
//transporter是发送电子邮件的一种方式
const transporter=nodemailer.createTransport({
服务:“gmail”,
认证:{
用户:Gmail,
密码:gmailPassword
}
});
常量邮件选项={
发件人:Gmail,
发送至:req.body.email,
主题:“完整堆栈开发人员”,
html:`Welcome${req.body.firstName}
非常感谢您与我联系。我希望您是
无论你身在何处,都会度过一段美好的时光
通过培训,我是一名完整的堆栈开发人员,目前我可以胜任一份普通的堆栈工作,这将挑战我做得更好
谢谢${req.body.firstName}`
};
transporter.sendMail(邮件选项);
})
//捕获404并转发到错误处理程序
应用程序使用((请求、恢复、下一步)=>{
下一步(createError(404));
});
//错误处理程序
应用程序使用((错误、请求、恢复、下一步)=>{
//设置局部变量,仅提供开发中的错误
res.locals.message=err.message;
res.locals.error=req.app.get('env')='development'?err:{};
//呈现错误页面
资源状态(资源状态| | 500);
res.render(“错误”);
});
exports.app=functions.https.onRequest(app);
您应该使用cors
包来解决cors问题。
在您的节点中,请设置cors
。
我可以向您展示我使用cors
发送邮件的firebase云功能。
顺便说一下,我使用了sendgrid
而不是node-mailer
。
我将通过显示我的代码来展示您应该如何实现cors
包装您的函数,因为我通常使用这种方式将我的函数打包为cors
exports.sendEmail = functions.https.onRequest((req, res) => {
if (!req.body) return res.sendStatus(400);
return cors(req, res, () => {
var to = req.body.to;
var from = req.body.from;
var sender_name = req.body.sender_name;
var message = req.body.message;
var pkgname = req.body.pkgname;
var html = "<h2>" + sender_name + "</h2>" + message;
const sgMail = require("@sendgrid/mail");
const subject = "Pre registration - " + pkgname + " - " + sender_name;
sgMail.setApiKey(
"sendgrid api key"
);
const msg = {
to: to,
from: from,
subject: subject,
html: html,
};
sgMail.send(msg, (sendError, sendRes) => {
if (sendError) return res.status(500).send(sendError);
else return res.status(200)(sendRes);
});
});
});
exports.sendmail=functions.https.onRequest((req,res)=>{
如果(!req.body)返回res.sendStatus(400);
返回cors(请求、回复,()=>{
var to=要求主体to;
var from=请求主体from;
var sender_name=req.body.sender_name;
var消息=req.body.message;
var pkgname=req.body.pkgname;
var html=“”+发件人名称+“”+消息;
const sgMail=require(@sendgrid/mail”);
const subject=“预注册-”+pkgname+“-”+发件人名称;
sgMail.setApiKey(
“sendgrid api密钥”
);
常数msg={
致:致,,
from:from,,
主题:主题,,
html:html,
};
sgMail.send(msg,(sendrerror,sendRes)=>{
如果(发送错误)返回res.status(500)。发送(发送错误);
否则返回res.status(200)(sendRes);
});
});
});
仅供参考,sendgrid是免费的 如果您使用cors中间件来处理这个问题,可能会更容易。在cors中间件中,检查请求方法
选项
并在设置标头后使用res.sendStatus(204)
发送204
。浏览器发送检查cors的选项请求。