表单POST请求体为空 - 的Node.js /快递/ EJS /身体解析器

问题描述 投票:2回答:3

我有在node.js中登记的形式与我的帖子麻烦每一个形式张贴在体内的请求时,有信息,但身体是空的。我是新来的节点,并表示因此它可能是小东西,但我不能为我的生命数字出来。

这是我在EJS形式:

<form class="form-signin" method="post" action="/users/register">
  <div class="form-group">
    <label class="sr-only" for="email">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Enter Email" autocomplete="off">
  </div>
  <div class="form-group">
    <label class="sr-only" for="password">Password</label>
    <input type="password" class="form-control" id="password" placeholder="Enter Password">
  </div>
  <div class="form-group">
    <label class="sr-only" for="confirmPassword">Confirm Password</label>
    <input type="password" class="form-control" id="confirmPassword" placeholder="Re-enter Password">
  </div>
  <button class="btn btn-dark mt-3" type="submit">Submit</button>
</form>

这里是用户路由器中我的路由器代码:

router.post('/register', function(request, response, next) {
  console.log(request);
  console.log(request.body)
  response.render('register', { 'title': 'Register' });
});

下面是在控制台上相关的输出:

console.log(request); output shortened this is just the tail.      
  _startAt: [ 299545, 483820242 ],
  _startTime: 2018-09-20T16:08:59.366Z,
  _remoteAddress: '::1',
  body: {},
  _body: true,
  length: undefined,
  read: [Function],
  secret: undefined,
  cookies: {},
  signedCookies: {},
  route:
   Route {
     path: '/register',
     stack: [ [Object] ],
     methods: { post: true } } }
{}
POST /users/register 200 21.906 ms - 2361

app.js代码:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var bodyParser = require('body-parser');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

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

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use(function(request, response, next) {
  next(createError(404));
});

app.use(function(error, request, response, next) {
  response.locals.message = error.message;
  response.locals.error = request.app.get('env') === 'development' ? error : {};
  response.status(error.status || 500);
  response.render('error');
});

module.exports = app;

事情我已经尝试:

  • 不同的浏览器
  • 不同的路线
  • 从头开始编写简单的形式
  • 强大的而不是身体的解析器

我在这一点上,它与EJS形式的问题,因为在请求体对象始终是空的相当肯定。

node.js forms express ejs body-parser
3个回答
1
投票

在形式<input>元件必须定义一个name属性,而不仅仅是id


0
投票

对方的回答中指出你错过了什么,我更新了.ejs代码向您展示发布HTML表单的正确方法:

<form class="form-signin" method="post" action="/users/register">
  <div class="form-group">
    <label class="sr-only" for="email">Email address</label>
    <input type="email" name="email" class="form-control" id="email" placeholder="Enter Email" autocomplete="off">
  </div>
  <div class="form-group">
    <label class="sr-only" for="password">Password</label>
    <input type="password" name="password" class="form-control" id="password" placeholder="Enter Password">
  </div>
  <div class="form-group">
    <label class="sr-only" for="confirmPassword">Confirm Password</label>
    <input type="password" name="confirmPassword" class="form-control" id="confirmPassword" placeholder="Re-enter Password">
  </div>
  <button class="btn btn-dark mt-3" type="submit">Submit</button>
</form>

您可以做到以上或处理与使用Javascript / jQuery的提交,如果你提交一个更复杂的形式或需要在域某种验证。

看到这个答案:Submit a form using jQuery


0
投票

另一个常见的错误使用快递时,请确保您使用boyParser i.e

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

router.use(bodyParser.json())
.use(bodyParser.urlencoded({
    extended: true
}));
© www.soinside.com 2019 - 2024. All rights reserved.