【问题标题】:Can't capture POST params with Node.js and Express无法使用 Node.js 和 Express 捕获 POST 参数
【发布时间】:2014-12-20 08:05:22
【问题描述】:

我正在尝试一个简单的Node.js + Express RESTful API 示例,并且我创建了一个简单的操作来模拟登录请求。这是我的代码:

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

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

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
module.exports = app;

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res) {
  res.send('respond with a resource');
});

/* My Login */
router.post('/login', function(req, res){
    var data = {
        user : req.body.user,
        pass : req.body.pass
    };
    res.send(data);
});

module.exports = router;

我正在使用 Postman 模拟 POST 请求(用户 = joe,pass = 123456),但我收到一个空的 {} 作为响应。已经尝试this solution,没有成功。如何在 Node.js 上捕获 POST 参数?怎么了?

更新 我已经嵌入了我的 app.jsusers.js 代码以便更好地查看。

【问题讨论】:

  • 这很奇怪,至少您应该收到带有空值或错误消息的对象。您的 NodeJS 是否输出任何错误日志?您确定您的路由路径工作正常吗?
  • 您使用的 Express 版本是什么?您需要使用 bodyParser 中间件。
  • 你在路由器之前设置了什么中间件?您也可以显示该代码吗?
  • @Hless 没有错误消息。该路线运行良好,例如,我可以打印Hello World。但是req.body.user 返回一个未定义的值。
  • 另外,console.log(req.headers['content-type']) 显示什么?

标签: javascript node.js express


【解决方案1】:

问题是您正在发送 multipart/form-data 请求,但您没有使用任何处理这些类型请求的中间件(Express 4 中的 body-parser 仅处理 application/x-www-form-urlencoded 和 JSON 请求)。

因此,您需要使用多部分解析模块,例如connect-busboy/busboymulter(Express 3 bodyParser-like API using busboy)、connect-multiparty/multipartyformidable(之前捆绑的 Express 3 bodyParser 中间件)或reformed(使用 busboy)。

【讨论】:

  • 我已经按照here 的描述安装了body-parser,但现在,我收到以下消息:Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
  • 就像我提到的,body-parser 不处理多部分,所以这对你没有帮助。虽然如果您不上传文件,您可以使用 body-parser 并将您的 Content-Type 设置为 application/x-www-form-urlencoded
  • Content-Type: application/x-www-form-urlencoded 非常适合我!
猜你喜欢
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多