(Express js 4 Multer)在文件上传之前检查表单字段(如果为空)

时间:2015-01-24 01:52:17

标签: node.js express multer

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

Upload.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){});

1 个答案:

答案 0 :(得分:1)

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

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