Post file from one server to another,using node.js , needle , busboy/multer

♀尐吖头ヾ 提交于 2019-12-02 04:50:37
Eenoku

I'd simply read your file from the first server with the function readFile() and then write it to the other server with the function writeFile().

Here you can see use of both functions in one of my servers.

I solved my problem by using the following code,

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);
    });
})

Server 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.");

});

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);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!