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

前端 未结 3 1643
囚心锁ツ
囚心锁ツ 2021-01-25 23:11

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 expe

相关标签:
3条回答
  • 2021-01-25 23:30

    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.

    0 讨论(0)
  • 2021-01-25 23:41
    '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.

    0 讨论(0)
  • 2021-01-25 23:53

    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);
    });
    
    0 讨论(0)
提交回复
热议问题