如何处理来自节点中具有多个字段的表单的表单数据?

How do I handle form data from a form with multiple fields in node?

我正在尝试建立一个页面,供人们回复婚礼网站。来宾输入他们的电子邮件地址,页面会找到出现在该邀请中的所有来宾,然后生成 table:

<%- include("./partials/header") %>
<h2>
    RSVP
</h2>




<div class="container">
    <div class=" row justify-content-center">
        <h5>Enter the email address that appears on your invitation.</h5>
    </div>
    <div class=" row justify-content-center">
        <div id="form-findGuest">
            <form action="/rsvp" method="GET">
                <input type="text" name="searchEmail" placeholder="email">
                <button type="submit">Submit</button>
            </form>
        </div>
    </div>
    
    <div class="row justify-content-center">
        <form action="/rsvp" method="POST">
            <table class="table">
                <% for(i=0;i<guests.length;i++){ %>
                    <tr>
                        <td><%= guests[i].name %></td>  
                        <% if(guests[i].isComing){ %>
                            <td>Attending</td>
                        <% } else { %>
                            <td>Not Attending</td>
                        <% } %>
                        <td>
                        <td>
                                <input type="checkbox">
                        </td>
                    </tr>
                <% }%>
            </table>
        </form>
    </div>
    <div class="row justify-content-center">
        <a href="/index" class="btn btn-secondary">Back</a>
    </div>
</div>



<%- include("./partials/footer") %>

问题是我不知道如何处理 post 路线。基本上我想做这样的事情:

app.post("/rsvp", (req, res)=>{
//  parse the req.body and for each rsvp that got sent here, add it to some array
//  now use a for loop to iterate through that array and change the isComing value for each guest
    res.redirect("index")
})

但我不知道如何解析 req.body,因为我不知道请求中将包含多少字段 - 一些电子邮件地址可能只对应一位客人,但其他人可能对应于,比如说,五个。

添加正文解析器(如果尚未添加),并利用

Object.keys

const express = require('express'),
  app = express(),
  bodyParser = require('body-parser');

// support parsing of application/json type post data
app.use(bodyParser.json());

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));
    
app.post("/rsvp", (req, res)=>{
var keys = Object.keys(req.body);
for(var i=0;i<keys.length;i++)
{
var guest=req.body[keys[i]];
//  now use this to change the isComing value for each guest
}

    res.redirect("index")
})