为什么我无法使用post方法从html表单获取数据?

时间:2019-04-05 06:17:01

标签: node.js express post get ejs

我已经使用ejs引擎获取和发布方法。当我使用http://localhost:8080/signup时,我将获得一个sigup表单,可以在其中输入我的值。问题是提交表单后,我无法控制台“ req.body”的值。帮帮我吗?

app.post('/signup',urlencodedParser,function(req,res){
    //console.log(req)
    var names = req.body;
    console.log(names);
    res.end("sigup submitted");
});
app.get('/signup', function(req, res) {///////////signup ejs loading
    res.render('signup')
});
<form id="signupForm" enctype="multipart/form-data" method="post" action="">
      <div class="container">
    <h1>Sign Up</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>
     <label for="name"><b>Company Name</b></label>
    <input type="text" placeholder="Company Name" name="name" required>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

    <label for="psw-repeat"><b>Address</b></label>
    <input type="text" placeholder="Address" name="address" required>


    <label>
      <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
    </label>

    <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

    <div class="clearfix">
      <button type="button" class="cancelbtn">Cancel</button>
      <button type="submit" class="signupbtn">Sign Up</button>
    </div>
  </div>
</form>

1 个答案:

答案 0 :(得分:0)

Express确实带有一些正文解析器,但是如果您使用multipart/form-data,则答案将是https://github.com/expressjs/multer而不是

您甚至可以读到https://www.npmjs.com/package/body-parser(又名 body-parser )也不读multipart/form-data,因为它们可能很复杂,只有在发送文件时才应使用。