1

我要检查表单字段是否为空,以及在上传文件之前是否更改了名称,但 multer 首先上传文件,然后填充 req.body。我正在使用 multer 作为中间件。

上传.js

checkField: function(req, res, next) {
   //Here i want to check if req.body.field1, field2 are empty or not
   //If empty res.send('Required fields are empty');
   console.log(req.body); //This returns empty {}
},
module.exports = {
uploadFile: multer({
    limits: {
        files: 1
    },
    dest: '/upload/',
})
};

路由器:

app.post('/upload', upload.checkField, upload.uploadFile, function(req, res){});
4

1 回答 1

1

如果您的目标是检查请求中发送的内容,则中间件顺序是错误的。在解析之前,您无法查看已解析的请求正文。试试这个顺序:

app.post('/upload', upload.uploadFile, upload.checkField, function(req, res){});
于 2015-01-24T06:21:46.760 回答