繁体   English   中英

我可以在使用 html、mysql 和 NODE.js 的一个按钮上使用 2 个 POST 请求吗?

[英]Can I use 2 POST requests on click of one button using html, mysql and NODE.js?

我想将表单详细信息保存到 MYSQL 数据库,并使用 nodemailer 将表单详细信息邮寄给管理员。 到目前为止,我能够将数据发送给管理员并将数据单独保存到 MySql 数据库,但我想要它做的是同时保存和发送表单详细信息。

    app.post('/send',function(req,res){

        var type_lea=req.body.lev_type.value
        res.write('type "' + req.body.lev_type.value+'".\n');
      let transporter = nodeMailer.createTransport({
          host: 'smtp.gmail.com',
          port: 465,
          secure: true,
          auth: {
              user: 'marcus1313@gmail.com',
              pass: 'Password'
          }
      });

    app.post('/submit',function(req,res){

      var employ_id=req.body.emp_id;
    var reason=req.body.leave_reason;
    var sql = "INSERT INTO leave_det (Emp_id,Reason) VALUES                                         
    ('"+req.body.emp_id+"','"+         req.body.leave_reason+"',)";
        conn.query(sql, function (err, result) {
          if (err) throw err;
          console.log("1 record inserted");

        });

在这个 html 代码中,我只能调用一个发布请求,但我想同时调用它们。

    <form action="/send" name="mail"  class="loyal" method="post" >
   </form>

您只能将 POST 请求发送到一个端点。 但这应该不是问题,因为您可以在将数据存储到数据库的同一 function 中调用您的邮件代码。

由于调用 SMTP 服务器需要一些时间,我将把邮件任务完全移出这里的循环。 只需写入数据库并在单独的后台处理工作中进行邮件发送。

还请仔细检查您的 SQL 声明以了解 SQL 注入的风险。

您可以尝试使用此方法可能会对您有所帮助

var app = express()

function otherPath(req, res, next) {
    return res.send('ok')
}

function home(req, res, next) {
    req.url = '/other/path'
  /* Uncomment the next line if you want to change the method 
    req.method = 'POST'
  */
    return app._router.handle(req, res, next)
}
app.get('/other/path', otherPath)
app.get('/', home)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM