0

so I have an express.js server, I don't quite understand how I can send and receive

I have (for example) an input for someone to write his email , I want the email to be sent to the server, validated, and then an email function sends an email to that user, However, my issue is that I don't know how to pass the email variable from client to the server, and I don't know how to trigger the email function as soon as the server validates the email either.

The only thing that I know to do with an express server is just routing Pages from. I tried AJAX but I think that's overkill because I don't understand it well and because its made to load data in a website without a reload so it has different kind of purpose.

French Noodles
  • 127
  • 1
  • 9

2 Answers2

1

On the client side there will be a form. That form will have an input field for the email address that should have some name name/id. When the form is submitted, that name id will be a part of the request object's body. Here's a link with more info

PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13
  • just a question, lets say i got the client side code ready, the form/body thing what do i do in the backend? how can i trigger a certain function? Is there an easier way to do this and im the one that is overcomplicating stuff? – French Noodles Nov 22 '20 at 17:32
  • 1
    After you have bodyParser set up as a middleware (check the link provided to see how to do that) then you should be able to access the email from the form as `req.body.email` (or whatever name the email address has on the form – PhantomSpooks Nov 22 '20 at 17:37
  • oh nice, but dont understand something, lets say i can access the email from the server, how can i remotely make the server use that variable and run a function on it from client side automatically – French Noodles Nov 22 '20 at 17:41
  • 1
    Lets say you have a website https://example.com/subscribe that has the form we've been talking about. In your express /subscribe route, you would pass `req.body.email` to a function that'd send it – PhantomSpooks Nov 22 '20 at 17:43
  • 1
    If you post your form I'll try to write out an example but I haven't used express in a while so it may be buggy – PhantomSpooks Nov 22 '20 at 17:46
  • oh okay ill do more research on that – French Noodles Nov 22 '20 at 17:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224929/discussion-between-phantomspooks-and-french-noodles). – PhantomSpooks Nov 22 '20 at 17:57
1

Lets say your form is something like this:

<form action="/subscribe" method="post">
<input type="text" name="email"/>
<input type="submit" />
</form>

In your express server you'd do something like this:

router.post('/subscribe',(req,res,next)=>{
if(!emailValidator(req.body.email)){
  // I would do email validations client side to but if you
  // want to do server side send some html saying the email is invalid
  res.sendFile(invalidEmail.html)
}
else{
  //I assume you have some script for sending email. I'll use nodemailer cuz its the first
  //module I found
  let sender = 'emailbot@website.com'
  let transporter = nodemailer.createTransport({
    service:'gmail',
    auth:{
      user:sender,
      pass:'password'
    }
  })
  let mailOptions = {
    from: sender,
    to: req.body.email,
    subject:'New sign up',
    text:'Thanks for subscribing'
  }
  transporter.sendMail(mailOptions,function(error,info){
    if(error){
      // do somehting
      console.log(error)
    }
    else{
      console.log('Sent new user email')
      req.next()
    }
  })
}
})
PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13
  • Thanks for the clarification!! but i dont understand something, how would i link the html part with this server part? Like how do i make it trigger this code when someone inputs an email – French Noodles Nov 22 '20 at 18:15
  • emailValidator is undefined – French Noodles Nov 22 '20 at 18:34
  • 1
    Oooh I made an error `
    ` should be `
    ` the action tells the form where to send the form to and is where the linking happens. And the reason emailValidator is null is because its pseudo code. You can install and import an email validator such as this [one](https://www.npmjs.com/package/email-validator)
    – PhantomSpooks Nov 22 '20 at 21:36
  • Thanks you very much! – French Noodles Nov 24 '20 at 17:46