1

我创建了 app.js 来启动服务器,该服务器也有调用前端 html 页面的代码。在 html 中,我添加了单击事件,它调用另一个 js 文件 ex。myAccountForm.js,在这个文件中我想使用 expressjs 来实现一些功能,如何实现?

4

1 回答 1

1

正如您已经表达为您的 Web 框架一样,在此处添加一个路由,该路由将监听您前端的点击。因此,当您单击该按钮时, - 它会在您的前端 JS 中调用一个函数,该函数将在您的快速服务器上调用此路由。假设路线是“/getaccountform”,它是一个 GET。将此路线添加为

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

在 myAccountForm.js 中,编写你的函数,比如 getMyAccountForm(),并通过将其附加到 module.exports 来公开它。喜欢

module.exports.getMyAccountForm=getMyAccountForm;

需要 myAccountForm.js,你的路线写成这样

var myAccountForm=require('myAccountForm.js 的路径');

所以你的代码应该像

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

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

在你的 getaccountform.js 中处理请求和响应我希望这对你有意义并且对你有用。如果没有,请添加有关您的问题的更多详细信息

于 2018-07-29T14:08:10.467 回答