使用node.js,needle,busboy/multer将文件从一台服务器发布到另一台服务器 [英] Post file from one server to another,using node.js , needle , busboy/multer

查看:248
本文介绍了使用node.js,needle,busboy/multer将文件从一台服务器发布到另一台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一台小型映像从一台服务器移动到另一台(都是正在运行的节点).当我搜索时,我还没有找到足够的东西. 帖子仍然没有答案.

I would like to move a small image from one server to another (both running node). As I search, I haven't found enough. This post remains unanswered.

在开始实验时,我将以下内容写入了第一台服务器:

As I started experimenting I wrote the following to the first server :

app.post("/move_img", function(req, res) {
    console.log("post handled");
    fs.readFile(__dirname + "/img_to_move.jpg", function(err, data) {
        if (err) throw err;
        console.log(data);
        needle.post(server2 + "/post_img", {
           data: data,
           name : "test.jpg"
        }, function(result) {
            console.log(result);
            res.send("ok");
        });
    });  
});

这部分似乎正常工作,因为我可以在同一服务器上写入数据(使用fs.writeFile)来重新创建img.

This part seems to be working as I could be writing the data in the same server (using fs.writeFile) recreate the img.

现在,当我尝试在其他服务器上处理帖子时,我遇到了问题.

Now as I am trying to handle the post in the other server I have a problem.

Server2:

app.post('/post_img', [ multer({ dest: './uploads/images'}), function(req, res) {

    console.log("body ",req.body) // form fields
    console.log("files ",req.files) // form files
    res.send("got it");
}]);

这样,我在文件中得到了一个空对象,在主体中得到了以下内容:{ 'headers[Content-Type]': 'application/x-www-form-urlencoded', 'headers[Content-Length]': '45009' }

This way i get an empty object in the files and the following in the body: { 'headers[Content-Type]': 'application/x-www-form-urlencoded', 'headers[Content-Length]': '45009' }

我认为我可以使用busboy作为替代,但是我无法使其正常工作.任何建议,教程都将受到欢迎.

I think I could use busboy as an alternative but I can't make it to work. Any advice, tutorial would be welcome.

推荐答案

我通过使用以下代码

server1(使用针):

server1 (using needle) :

app.post("/move_img", function(req, res) {
    console.log("post handled")

    var data = {
        image:{
        file: __dirname + "/img_to_move.jpg",
        content_type: "image/jpeg"}
    }

    needle.post(server2 + "/post_img", data, {
        multipart: true
    }, function(err,result) {
        console.log("result", result.body);
    });
})

服务器2:

app.use('/post_img',multer({
    dest: '.uploads/images',
    rename: function(fieldname, filename) {
        return filename;
    },
    onFileUploadStart: function(file) {
        console.log(file.originalname + ' is starting ...')
    },
    onFileUploadComplete: function(file) {
        console.log(file.fieldname + ' uploaded to  ' + file.path)
    }
}));

app.post('/post_img', function(req, res) {

    console.log(req.files);
    res.send("File uploaded.");

});

服务器1的替代方法如下(使用表单数据模块):

An alternative for the server 1 is the following (using form-data module):

var form = new FormData();
form.append('name', 'imgTest.jpg');
form.append('my_file', fs.createReadStream(__dirname + "/img_to_move.jpg"));

form.submit(frontend + "/post_img", function(err, result) {
    // res – response object (http.IncomingMessage)  //
    console.log(result);
});

这篇关于使用node.js,needle,busboy/multer将文件从一台服务器发布到另一台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆