0

Right now I have a POST form submission working, on my home page, for signing up someone for a mailing list. Now I'm trying to figure out how to "confirm" the success of the signup. Here is the post code:

// handle a signup submission
app.post('/signup', function(req, res) {

    // create the signup email message
    const message = {
        to: 'contact@example.com',
        from: req.body.email,
        subject: 'Example.com signup',
        text: req.body.email + ' has been subscribed'
    }

    // send the email and handle any errors
    sendgrid.send(message, function(err, result) {
        // handle errors
    });

    // redirect back to the original page
    res.redirect('/');
});

Is there a way to communicate a message to my home page so that I could, for example, have a pop-up appear for a few seconds ("Subscription successful!") or something to that effect?

My home page is an index.html file. I'm open to converting it over to .ejs file if necessary.

The top answer here looks like it could work: How do I use req.flash() with EJS? . Is that the "right" way of doing it?

wsaxton
  • 1,030
  • 15
  • 34
  • as much as I know that is something to handle on the client side, so you have to send a code or some answer to the client to inform success, and if you got a success response from server, you can add a pop-up in webpage. – ganjim Nov 07 '17 at 21:47

1 Answers1

1

I believe connect-flash should solve your problem, it's now your choice on which templates engine you wanna use to render your site, jade, ejs etc are options that you could think of using.

You can check this git repo, npm usage of connect-flash and the npm usage of express-flash, they might be of help to you.

antzshrek
  • 9,276
  • 5
  • 26
  • 43