简体   繁体   中英

Node.js change HTML code with a callback function

I want to display a confirm message using HTML once I have sent an email via nodemailer. I am fairly new to node so I'm not sure if I'm missing something obvious.

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
      ...
  });

  var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          return console.log(error);
      }
      console.log('Message sent: ' + info.response);

      ... (Something here that is going to print a confirmation message  to the html code of the page)
  });

  res.end("yes");
});

Thanks

You should move the call to res.end() inside the sendMail() callback, for example:

app.post("/bye", function (req, res) {
    // send e-mail
    var transporter = nodemailer.createTransport({});

    var mailOptions = {};
    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
            console.log(error);

            res.status(500).send({
                message: 'Unable to send mail'
            });
        }

        console.log('Message sent: ' + info.response);

        res.status(200).send({
            message: 'Mail sent successfully'
        });
    });
});

And then you need to handle this message on the client side to present an okay or failed message to the user.

Node runs entirely separately from the browser, so you cannot directly modify HTML within the browser from node. You need to send a response from node to the browser, then deal with that response within the browser. It might work something like this...

Node

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
  ...
  });

    var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          console.log(error)
          res.end({
              success: false, 
              msg: error
          });
      }

      console.log('Message sent: ' + info.response);

      res.end({
          success: true
      });
  });
});

Then in your client javascript (using jQuery post for simplicity, but plain javascript requests, or another library if you prefer)...

$.post('/bye').then(function(response) {
    if (response.success) {
        $('#msg').text('Your message was sent');
    } else {
        $('msg').text(response.msg);
    }
}).catch(function(err) {
    $('#msg').text('Could not reach server');
});

the res is the object send to the client back.

you can write your message with:

res.send('your message');

and i would remove your res.end("yes"); at the end. it will send the response maybe before you finished with sending the mail.

so your code would be the following:

app.post("/bye", function(req, res){
  // send e-mail
  var transporter = nodemailer.createTransport({
      ...
  });

  var mailOptions = {
      ...
  }

  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          res.status(500).end('error');
          return console.log(error);
      }
      console.log('Message sent: ' + info.response);

      res.status(200).send('your message');
  });
});

Tutorialspoint has a good introduction for express. maybe it can help you.

也许您可以将res.end("...")放入.sendMail(... function() {而不是之后。

if you want to show a whole html-page you could send and html file to the browser:

  res.sendfile('views/test.html', {root: __dirname }) 

Otherwise if you want to display an error message on the current html page you should send json data to the client containing the message, message type and so on,parse the json in the client and render it:

 res.send(JSON.stringify({message: 'message' , type: 'success'}); 

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