Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 尝试使用nodejs同时使用bodyParser和busboy_Node.js_Express_Body Parser_Busboy - Fatal编程技术网

Node.js 尝试使用nodejs同时使用bodyParser和busboy

Node.js 尝试使用nodejs同时使用bodyParser和busboy,node.js,express,body-parser,busboy,Node.js,Express,Body Parser,Busboy,我是一个试图学习nodejs的noob 我的应用程序需要发布、放置和上载文件。 我发现您无法使用BodyParser上载文件 在我仅仅使用BodyParser(在应用程序级别添加它)之前,POST和PUT是有效的。(当然上传没有) 我找到了这篇文章 [ 现在试着跟随这篇文章。我只是试着得到文章,并把工作,然后移动到上传。对于放置和张贴,我没有得到一个错误,但我的req.body是{} 在我的server.js中,我有 mongoose.connection.on("connected", fun

我是一个试图学习nodejs的noob 我的应用程序需要发布、放置和上载文件。 我发现您无法使用BodyParser上载文件

在我仅仅使用BodyParser(在应用程序级别添加它)之前,POST和PUT是有效的。(当然上传没有)

我找到了这篇文章 [

现在试着跟随这篇文章。我只是试着得到文章,并把工作,然后移动到上传。对于放置和张贴,我没有得到一个错误,但我的req.body是{}

在我的server.js中,我有

mongoose.connection.on("connected", function(ref) {
    console.log("Connected to DB!");

    var app = express();

    port = process.env.port || 3000;
    ip = process.env.ip;

    var router = express.Router();
    router.use(function(req, res, next) {
        next(); 
    });

    app.use('/api', router);

    require('./app/routes')(router);


    app.listen(port, ip, function() {
        console.log('listening on port ' + port);
    });
});
在我的router.js中,我有(删除一些混乱)


解决方案
问题是我缺少bodyParser.json()。我已使用有效的代码更新了说明

问题是我缺少bodyParser.json()。我已使用有效的代码更新了说明

var ImageModel = require('./models/image.js')
    ,common = require('./common.js')
    ,bodyParser = require('body-parser')
    ,busboy = require('connect-busboy')
; 

module.exports = function(router) {

router.put('/pict/:id',
    bodyParser.urlencoded({ extended: true }),
    function (req, res) {
    console.log("--> %j", req.body);
    ImageModel.findByIdAndUpdate(req.params.id, req.body, function (err, user) {
        if (err) throw err;

        ImageModel.findById(req.params.id, function (err, pict) {
            if (err) res.send(err);
            res.json(pict);
        });

    });
});

router.post('/pict',
    bodyParser.urlencoded({ extended: true }),
    function (req, res) {
    console.log("--> %j", req.body);
    var pict = new ImageModel(req.body);
    pict.save(function (err) {
        if (err) throw err;
        res.json(pict);
    });
});
router.put('/pict/:id',
    bodyParser.urlencoded({ extended: true }), bodyParser.json(),
    function (req, res) {
    console.log("--> %j", req.body);
    ImageModel.findByIdAndUpdate(req.params.id, req.body, function (err, user) {
        if (err) throw err;

        ImageModel.findById(req.params.id, function (err, pict) {
            if (err) res.send(err);
            res.json(pict);
        });

    });
});

router.post('/pict',
    bodyParser.urlencoded({ extended: true }), bodyParser.json(),
    function (req, res) {
    console.log("--> %j", req.body);
    var pict = new ImageModel(req.body);
    pict.save(function (err) {
        if (err) throw err;
        res.json(pict);
    });
});