0

I have created app.js to start the server which also have code for calling front end html page. In html, I have added click event where it calls another js file ex. myAccountForm.js, in this file I want to use expressjs for happening some functionality, How this can be achieved?

1 Answers1

0

As you have express as your web framework, add a route there, which would listen to the click from you front end. So when you click on that button, - it will invoke a function in your front side JS, which would call this route at your express server. Lets say the route is '/getaccountform' which is a GET. Add this route as

app.get('/getaccountform',(req,res)=>{
//call the function in myAccountForm.js,
});

in myAccountForm.js, write your function say getMyAccountForm(), and expose it by attaching it to module.exports. like

module.exports.getMyAccountForm=getMyAccountForm;

require myAccountForm.js, where your route is written like

var myAccountForm=require('path to your myAccountForm.js');

So your code should be like

 var myAccountForm=require('path to your myAccountForm.js');

 app.get('/getaccountform',(req,res)=>{
     getMyAccountForm(req,res);
 });

Handle the request and response in your getaccountform.js I hope this makes sense and works for you. If not, please add more details about your issue

xan_z
  • 226
  • 1
  • 9