为什么在expressjs中对POST请求清空req.body?

时间:2018-06-03 08:09:18

标签: node.js express

我是表达新手。我正在使用POST请求将数据从Web表单发送到另一个表单(或在控制台上显示)。但我收到一个空物。我的路线位于处理数据的单独文件夹中。我使用body-parser作为中间件来处理请求体。

这是一个简单的网络表单

<!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="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
    <title>Login</title>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <h1>Login</h1>
        </div>
        <form method="POST" class="col-md-8" action="/profile">
            <div class="form-group row">
                <div class="col-md-4">
                    <label for="email" class="form-control border-0">Email</label>
                </div>
                <div class="col-md-8">
                    <input type="email" class="form-control" id="email" placeholder="Enter email">
                </div>
            </div>
            <div class="form-group row">
                <div class="col-md-4">
                    <label for="password" class="form-control border-0">Password</label>
                </div>
                <div class="col-md-8">
                    <input type="password" class="form-control" id="password" placeholder="Enter Password">
                </div>
            </div>
            <button type="submit" class="btn btn-primary">Login</button>
        </form>
    </div>
    
</body>
</html>

这是路径文件(route.js)

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

var jsonParser = bodyParser.json(); // support json encoded bodies
var urlencodedParser = bodyParser.urlencoded({ extended: false }); // support encoded bodies

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

router.get('/login', (req, res)=>{
  res.render('auth/login');
});

router.post('/profile', urlencodedParser, function(req, res){
  res.send("THank you");
  console.log("called post");
  console.log(req.body);
});

module.exports = router;

这是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 jsonParser = bodyParser.json(); // support json encoded bodies

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

var app = express();



// view engine setup
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('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});


module.exports = app;

1 个答案:

答案 0 :(得分:1)

您需要将name属性设置为HTML <input>标记,例如:

<input name="email" type="email" class="form-control" id="email" placeholder="Enter email">