Node.js 此发送重置密码电子邮件的实现是否正确?
我阅读了这篇文章,了解了如何在express中重置密码Node.js 此发送重置密码电子邮件的实现是否正确?,node.js,express,nodemailer,Node.js,Express,Nodemailer,我阅读了这篇文章,了解了如何在express中重置密码 mailer.js const nodemailer = require("nodemailer") export const transporter = nodemailer.createTransport({ service: "gmail", auth: { user: process.env.EMAIL, pass: process.env.PASSWORD } })
mailer.js
const nodemailer = require("nodemailer")
export const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
})
export const getPasswordResetURL = (user, token) => {
`http://localhost:3000/password/reset/${user._id}/${token}`
}
export const resetPasswordTemplate = (user, url) => {
const from = process.env.EMAIL
const to = user.email
const subject = "Password Reset"
const html = `
<p>Hey ${user.name || user.email},</p>
<p>We heard that you forgot your password. Sorry about that!</p>
<p>But don’t worry! You can use the following link to reset your password:</p>
<a href=${url}>${url}</a>
<p>If you don’t use this link within 1 hour, it will expire.</p>
`
}
return { from, to, subject, html }
这给了我一个意外的错误{在这行中
从“./utils/mailer”导入{transporter,getPasswordResetURL,resetPasswordTemplate} 我是否可以将此导出常量转换为模块。导出并通过以下方式要求上述内容:
const{transporter,getPasswordResetURL,resetPasswordTemplate}=require(“../utils/mailer”)如问题中所述,替换所有导出const并将其替换为
module.exports = {transporter, getPasswordResetURL, resetPasswordTemplate}
同时更换管路
import { transporter, getPasswordResetURL, resetPasswordTemplate } from "../utils/mailer"
到
导入时,export是es6语法,require是,module.exports是es5语法。如果要使用import,则需要配置Babel
建议
最有可能的例子是,他同时使用es6和es5语法(他一定配置了babel).但在您的情况下,我认为您尚未配置babel。因此,我建议您要么配置babel,要么将所有es6语法更改为es5。在es6中,您可以使用es5,但在es5中,您不能使用es6,如您在问题中所述,替换所有导出常量并将其替换为
module.exports = {transporter, getPasswordResetURL, resetPasswordTemplate}
同时更换管路
import { transporter, getPasswordResetURL, resetPasswordTemplate } from "../utils/mailer"
到
导入时,export是es6语法,require是,module.exports是es5语法。如果要使用import,则需要配置Babel
建议
最有可能的例子是,他同时使用es6和es5语法(他必须配置了babel)。但在你的情况下,我认为你没有配置babel。因此,我建议你要么配置babel,要么将所有es6语法更改为es5。在es6中,你可以使用es5,但在es5中,你不能使用es6import{transporter,getPasswordResetURL,resetPasswordTemplate}来自“./utils/mailer”这是es6语法。将其更改为es5
return{from,to,subject,html}
。我们为什么要这样返回?很可能是您从他同时使用es6和es5语法中得到的示例(他必须配置了babel)。但在您的情况下,我认为您没有配置babel。因此,我建议您要么配置babel,要么将所有es6语法更改为es5。在es6中,您可以使用es5,但在es5中,您不能使用es6。好的,我已经配置了babel。您可以在中配置相同的,然后我们将处理它。导入{transporter,getPasswordResetURL,resetPasswordTemplate}来自“./utils/mailer”这是es6语法。将其更改为es5return{from,to,subject,html}
。我们为什么要这样返回?很可能是您从他同时使用es6和es5语法中得到的示例(他必须配置了babel).但在您的情况下,我认为您没有配置babel。因此,我建议您要么配置babel,要么将所有es6语法更改为es5。在es6中,您可以使用es5,但在es5中,您不能使用es6。好的,我已经配置了babel。您可以在中配置相同的语法,然后我们将处理它。
const { transporter, getPasswordResetURL, resetPasswordTemplate } = require("../utils/mailer")