尝试从快递3更新到表达4我的文件上传路径开始返回req.files为undefined.我已将中间件安装为单独的依赖项,因为它们未包含在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的一些改变正在影响这个,但我还没有找到任何东西.有任何想法吗?
作为3.x
- > 4.x
更改的一部分,处理multipart/form-data
请求正文数据的bodyParser
中间件已从中间件中删除,因此它只解析application/x-www-form-urlencoded
和application/json
请求正文数据.
如果要将其multipart/form-data
用作请求主体,则需要使用multer
中间件.