没有从 NodeJs 中的 PugJS 表单中获取数据

Not getting the from data from PugJS form in NodeJs

我正在尝试添加一个身份验证表单,用户可以在其中输入他们的凭据,如果他们的凭据正确,我想将他们重定向到一个路由。因此,我创建了一个中间件 login.js,它呈现哈巴狗模板和 post 表单提交请求。 post 方法有效,但我没有在 req.body 中获取 pug 表单的值。我该怎么办?

这是 admingLogin.pug 文件

doctype html
html(lang='en')
  head
    meta(charset='UTF-8')
    meta(name='viewport', content='width=device-width, initial-scale=1.0')
    meta(http-equiv='X-UA-Compatible', content='ie=edge')
    link(rel='icon', href='../client/assets/favicon/login.ico', type='image/x-icon')
    link(rel='stylesheet', href='https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css')
    title Admin
  body
    .ui.middle.aligned.center.aligned.grid
      .column
        form.ui.large.form(action='/auth' method='post')
          .ui.stacked.secondary.segment
            .field
              .ui.left.icon.input
                p Log-in as an admin to create custom links
            .field
              .ui.left.icon.input
                i.user.icon
                input(type='text', name='username', placeholder='Username')
            .field
              .ui.left.icon.input
                i.lock.icon
                input(type='password', name='password', placeholder='Password')
            button(type="submit").ui.fluid.large.teal.submit.button Login
          .ui.error.message
    style.
      body>.grid {
      height: 100% !important;
      }
      .image {
      margin-top: -100px !important;
      }
      .column {
      max-width: 450px !important;
      margin-left: 55px !important;
      margin-right: 55px !important;
      }
      body {
      background: #30E8BF;
      background: -webkit-linear-gradient(to right, #FF8235, #30E8BF);
      background: linear-gradient(to right, #FF8235, #30E8BF);
      }

这是link to the complete project and the link to the route is here. The middleware is passed here

来自您的 HTML 的 POST 被发送为 application/x-www-form-urlencoded。您需要配置 body-parser 来处理这种类型。

目前,根据您添加的代码,它只能处理application/json

尝试在下面添加以下行 app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: true }));