Home > Net >  How can I connect front-end js with back-end js
How can I connect front-end js with back-end js

Time:09-19

I'm currently working on a website and I need to let the user send an email from that website. (The email should be sent through a designated email). So I'm going to use nodemailer, but since the JS is connected to the HTML-Elements, I can't import any libraries. So I created another JS file that runs back-end, but how do I connect the two? For the “JS Events” it would have to be connected to an HTML-Element.. And well, just calling a function doesn't work. Things like requireJS don't seem like a good choice either since I'd like to handle it back-end.

CodePudding user response:

The two options for communicating between client-side JavaScript and server-side code are:

  • HTTP requests
  • WebSockets

Approaches for using them include Forms, (if you are using Node.js for your backend then you are probably using Express, in which case this is a more specific tutorial), Ajax, the WebSocket API, and Socket.io.

For your purposes, forms are the simplest option. Ajax is slightly more complicated but you might prefer the extra flexibility in UX.

  • Related