Ryan_
Ryan_

Reputation: 64

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

Upvotes: 0

Views: 1549

Answers (5)

chresse
chresse

Reputation: 5805

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.

Upvotes: 0

Andreas Rau
Andreas Rau

Reputation: 336

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'});

Upvotes: 0

HollyPony
HollyPony

Reputation: 847

Maybe you could your res.end("...") into your .sendMail(... function() { instead of after.

Upvotes: 0

Alex Young
Alex Young

Reputation: 4039

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');
});

Upvotes: 1

Hyddan
Hyddan

Reputation: 1337

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.

Upvotes: 1

Related Questions