【问题标题】:Html button click and nodejs in javascriptjavascript中的HTML按钮单击和nodejs
【发布时间】:2019-01-05 22:23:03
【问题描述】:

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

【问题讨论】:

    标签: node.js ajax express node-fetch


    【解决方案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 中处理请求和响应 我希望这是有道理的,对你有用。如果没有,请添加有关您的问题的更多详细信息

    【讨论】:

      猜你喜欢
      • 2011-06-16
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2013-07-23
      相关资源
      最近更新 更多