Nodejs Express:从Postman发送图片到Multer

时间:2017-06-18 19:48:31

标签: node.js express file-upload postman multer

我有这条路线:

var express = require('express');
var multer = require('multer');

const upload = multer();

module.exports = (function () {

    var router = express.Router();

    router.post('/avatar/:user', upload.single('avatar'), function (req, res, next) {
        var file = req.file; // file passed from client
        var meta = req.body; // all other values passed from the client, like name, etc..
        console.log(file);
        console.log(meta);
        res.json({
            success: true
        });
    });

    return router;
})();

当我尝试使用带有标题'Content-Type:application / x-www-form-urlencoded'并在body'form-data'文件(图像)中使用Postman发布时,表达式崩溃并出现错误:< / p>

Error
    at readStream (F:\Web\WorkoutFocus_Server\node_modules\raw-body\index.js:196
:17)
    at getRawBody (F:\Web\WorkoutFocus_Server\node_modules\raw-body\index.js:106
:12)
    at read (F:\Web\WorkoutFocus_Server\node_modules\body-parser\lib\read.js:76:
3)
    at urlencodedParser (F:\Web\WorkoutFocus_Server\node_modules\body-parser\lib
\types\urlencoded.js:115:5)
    at Layer.handle [as handle_request] (F:\Web\WorkoutFocus_Server\node_modules
\express\lib\router\layer.js:95:5)
    at trim_prefix (F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\i
ndex.js:317:13)
    at F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\index.js:284:7

    at Function.process_params (F:\Web\WorkoutFocus_Server\node_modules\express\
lib\router\index.js:335:12)
    at next (F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\index.js
:275:10)
    at jsonParser (F:\Web\WorkoutFocus_Server\node_modules\body-parser\lib\types
\json.js:112:7)
    at Layer.handle [as handle_request] (F:\Web\WorkoutFocus_Server\node_modules
\express\lib\router\layer.js:95:5)
    at trim_prefix (F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\i
ndex.js:317:13)
    at F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\index.js:284:7

    at Function.process_params (F:\Web\WorkoutFocus_Server\node_modules\express\
lib\router\index.js:335:12)
    at next (F:\Web\WorkoutFocus_Server\node_modules\express\lib\router\index.js
:275:10)
    at cors (F:\Web\WorkoutFocus_Server\node_modules\cors\lib\index.js:185:7)

这是否与文件大小或multipart / form-data有关?

我尝试将bodyparser.json限制设置为'50mb',但没有区别。

0 个答案:

没有答案