Run code Node by pressing a button in html

Asked

Viewed 175 times

-1

Good morning gentlemen, I am beginner in Node and Javascript in general, I am using express.js and I want to press the button "SEND" it runs a code (that serves to send an email):

Print do meu HTML

The code Node I want to run when pressing the button:

const nodemailer = require('nodemailer')

function emailSend() {
  let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    auth: {
      user: "[email protected]",
      pass: "senha321" //nao 'e meu email kkkkkk

    }
  })

  transporter.sendMail({
    from: "Herbert Richard <[email protected]>",
    to: "[email protected]",
    subject: "Teste de envio de email",
    text: "Ola, sou hebert richard e estou testando o nodemailer"
  }).then(message => {
    console.log(message);
  }).catch(err => {
    console.log(err);
  })
}

My express:

const express = require('express')
const app = express()

app.get("/", function(req, res){
    res.sendFile(__dirname + '/src/index.html')
})

app.get("/style.css", function(req,res){
    res.sendFile(__dirname + "/src/style.css")
})

app.get("/runeterra-logo.png", function(req, res){
    res.sendFile(__dirname + '/src/runeterra-logo.png')
})

app.get("/wallpaper.jpg", function(req, res){
    res.sendFile(__dirname + '/src/wallpaper.jpg')
})

var server = app.listen(3000);

1 answer

0

Good morning buddy, first you are mixing things a little, no it is for the backend, what you want is that when you click a certain button it sends the request for the backend can send the email.

On your button you will put a function to call this request on the frontend, I advise you to use Xios (https://www.npmjs.com/package/axios) well done:

axios.get('/sendemail')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })

Certain that this above code should be applied to the onClick method on the button

Browser other questions tagged

You are not signed in. Login or sign up in order to post.