简体   繁体   中英

Nodejs Error: ENOENT: no such file or directory, stat

Sending email with the templates using ejs template for rendering the content but facing this issue when send email getting ENOENT: no such file or directory, stat

emailcontroller.js

sendEmail: function (req, res, next) {
    try {
        if (req.body.email) {
            var templateDir = path.join(__dirname, 'template','emailView');
            var temp = new EmailTemplate(templateDir);
            let transporter = nodemailer.createTransport({
                 host: 'smtp.gmail.com',
                port: 587,
                secure: false, // 
                auth: {
                  user: 'user@gmail.com', // generated ethereal user
                  pass: 'user12345' // generated ethereal password
                },
                tls: {
                    rejectUnauthorized: false
                }
            });
            var locals = {
                username: "User" //dynamic data for bind into the template
            };
            temp.render(locals, function (err, result) {
                if(err)
                console.log("err",err);
                else {
                        let mailOptions = {
                            from: 'user@gmail.com', // sender address
                            to: req.body.email, // list of receivers
                            subject: 'Welcome ✔', // Subject line
                            html:result.html,
                            text:result.text

                        };
                        transporter.sendMail(mailOptions, (error, info) => {
                            if (error) {
                                res.status(500).send(JSON.stringify({
                                    "status": "failed",
                                    "statuscode": res.statusCode,
                                    "message": error,
                                    "data": null
                                }));
                            } else {
                                res.status(200).send(JSON.stringify({
                                    "status": 0,
                                    "statuscode": res.statusCode,
                                }));

                            }
                        });
                    }
                })

            }

    } catch (err) {
        res.status(500).send(JSON.stringify({
            "status": 2,
            "statuscode": res.statusCode,
            "message": err.message,
            "data": null
        }));
    }

},

My Project structure

D:\\xampp\\htdocs\\myproject\\controller\\template

in which emailView.ejs file under template folder

also add in app.js

app.engine('html',require('ejs').renderFile);
app.set('view engine', 'ejs');
app.set('view engine', 'html');

i have found many solution but did not work 1. i removed emailView

( eg :var templateDir = path.join(__dirname, 'template');)

but in which i got

Neither html nor text template files found or are both empty in path template

To troubleshoot, I would try placing the template file in the same directory that the code resides in and move it to where it needs to be when you figure out the issue. No doubt it is a path issue.

It works,i have changed filename to html.ejs and remove 'emailView'
OLD

  var templateDir = path.join(__dirname, 'template','emailView'); 

NEW

  var templateDir = path.join(__dirname, 'template'); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM