【问题标题】:Updated application from express 3 to express 4, now req.files is returning undefined将应用程序从 express 3 更新为 express 4,现在 req.files 返回 undefined
【发布时间】:2014-04-21 00:49:59
【问题描述】:

在尝试从 express 3 更新到 express 4 时,我的文件上传路由开始返回未定义的 req.files。我已经将中间件安装为单独的依赖项,因为它们不包含在 express 中,并且我已经停止使用已删除的 app.configure() 方法。这是我的主服务器文件

    // dependencies ==========================================================================

    var express        = require('express');                  // framework
    var mongoose       = require('mongoose');                 // mongo driver
    var morgan         = require('morgan');                   // request logger
    var bodyParser     = require('body-parser');
    var methodOverride = require('method-override');
    var fs             = require('fs');
    var app            = express();                           // initialize as 'app'

    // configuration =========================================================================

    mongoose.connect('mongodb://localhost/constellates');

    var port = 4000;                                          // set port number for app
    app.use(bodyParser());                                    // pull information from html in POST
    app.use(express.static(__dirname + '/public'));           // set static files directory
    app.use(morgan('dev'));                                   // log every request to the console
    app.use(methodOverride());                                // simulate PUT and DELETE


    // routes ================================================================================

    require('./routes')(app);                                 // load routing from 'routes.js'

    // start server ==========================================================================

    app.listen(port);                                         // start server listening
    console.log('Server is listening on port ' + port);       // display success message with port number

这是我为故障排除而简化的路线

    app.post('/api/upload', function (req, res) {
      console.log(req.files);
    });

客户端没有任何变化。我最好的猜测是对 bodyParser 的一些更改正在影响这一点,但我还没有发现任何东西。有什么想法吗?

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    作为3.x -> 4.x 更改的一部分,用于处理multipart/form-data 请求正文数据的中间件已从bodyParser 中间件中删除,因此它仅解析application/x-www-form-urlencodedapplication/json 请求正文数据.

    如果你想使用multipart/form-data作为请求体,你需要使用multer中间件。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2014-05-31
      • 2016-06-28
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多