How to pass multipart request from one server to another in NodeJS?

喜你入骨 提交于 2020-04-16 03:41:25

问题


I have two nodeJS servers, Server 1 gets requests from the client and passes it to server 2 which returns a response to server 1 and it responds to the client. The client uploads a file and it has to be passed the same way as any other rest request that I have.

I use axios on server 1 to send the data to server2 and multer on server 2 to store the file on disk.

I have an issue sending the request from server1 to server2 because the body of the request contains nothing and the request is Multipart.

How should I handle the request on Server 1 ???

router.post('/fileUpload', (req, res) => {
    console.log(req.body);
    res.status(200).json({ msg: "Got file" });
});

回答1:


You can use form-data module to send multipart/form-data from nodejs application

Here is the code you can implement on Server1 to receive image file from client and send it to Server2.

const express = require("express");
const app = express();
const bodyParser = require('body-parser');
var multer  = require('multer')();
const FormData = require('form-data');
const axios = require('axios');
const fs = require('fs');

app.use(bodyParser.json());

app.post('/fileUpload' , multer.single('fileFieldName'), (req , res) => {
    const fileRecievedFromClient = req.file; //File Object sent in 'fileFieldName' field in multipart/form-data
    console.log(req.file)

    let form = new FormData();
    form.append('fileFieldName', fileRecievedFromClient.buffer, fileRecievedFromClient.originalname);

    axios.post('http://server2url/fileUploadToServer2', form, {
            headers: {
                'Content-Type': `multipart/form-data; boundary=${form._boundary}`
            }
        }).then((responseFromServer2) => {
            res.send("SUCCESS")
        }).catch((err) => {
            res.send("ERROR")
        })
})

const server = app.listen(3000, function () {
    console.log('Server listening on port 3000');
});

Here multer is used to handle the uploaded file




回答2:


 const formData = new FormData();
 formData.append('query', updateDocQuery);
 formData.append('variables', JSON.stringify(this.setUpdateDocParams() || {}));
 for (let i = 0; i < fileArr.length; i++) {
            formData.append(`file${i}`, fileArr[i])
        }

you can append your query variables and file to formData and pass formData to body




回答3:


@Apar Adhikari, your solution absolutely works, but I think we should use disk storage in multer instead of buffer. Because if we use buffer storage, RAM on server 1 will be soon overload when there are many requests. There is one more issue that is: if the request from server 1 to server 2 takes a long time, the request from the browser to server 1 will be timeout. So I think we should stream the multi part data from server 1 to server 2 immediately without storing it on server 1 temporary (both disk storage and buffer) or if possible, we should send the file to server 2 directly.



来源:https://stackoverflow.com/questions/52963648/how-to-pass-multipart-request-from-one-server-to-another-in-nodejs

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