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

3

1

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

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

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' }

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

Casebook answered 20/5, 2015 at 12:38 Comment(2)
That's my problem i don't know, where to start. I should use needle to post to another server holding the img in the post data, or should i look into something else?Casebook
@vanadium i would prefer to do it programmatically and not to use linux commands in my code. (if I understand correctly the solution you provide)Casebook
C
2

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);
});
Casebook answered 21/5, 2015 at 13:51 Comment(0)
C
0

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.

Cindelyn answered 20/5, 2015 at 12:45 Comment(1)
This could be done. How about the transition of the files from one server to another. p.s. I am editing my question.Casebook
S
0
'use strict';

const express   = require('express');
const multer= require('multer');
const concat = require('concat-stream');
const request = require('request');

const router = express.Router();

function HttpRelay (opts) {}


HttpRelay.prototype._handleFile = function _handleFile (req, file, cb) {
    file.stream.pipe(concat({ encoding: 'buffer' }, function (data) {
        const r = request.post('/Endpoint you want to upload file', function (err, resp, body) {

            if (err) return cb(err);
            req.relayresponse=body;
            cb(null, {});
        });

        const form = r.form();
        form.append('uploaded_file', data, {
            filename: file.originalname,
            contentType: file.mimetype
        });
    }))
};

HttpRelay.prototype._removeFile = function _removeFile (req, file, cb) {
    console.log('hello');
    cb(null);
};

const relayUpload = multer({ storage: new HttpRelay() }).any();

router.post('/uploadMsgFile', function(req, res) {
    relayUpload(req, res, function(err) {

        res.send(req.relayresponse);

    });
});

module.exports = router;

see multer does all the tricks for you. you just have to make sure you use no middle-ware but multer to upload files in your node starting point. Hope it does the tricks for you also.

Superabundant answered 5/4, 2020 at 5:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.