如何将文件上传请求转发到Node.js中的另一个域?

时间:2019-12-03 04:55:36

标签: node.js redirect file-upload request form-data

如何将上传请求转发到另一个域?

UI会将上传请求发送到

curl -X POST \
  http://localhost:4000/uploadFile \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 814459' \
  -H 'Host: localhost:4000' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F attachment1=@/C:/Users/superman/Desktop/tester.pdf

需要附加2个附加标头并将请求转发给

curl -X POST \
  http://anotherdomain.com/anotherUploadFile \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'newheader1: headervalue1' \
  -H 'newheader2: headervalue2' \
  -H 'Content-Length: 814459' \
  -H 'Host: anotherdomain.com' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F attachment1=@/C:/Users/superman/Desktop/tester.pdf

1 个答案:

答案 0 :(得分:0)

首先,您将图像保存在第一个域中,然后您将发起一个新的发布请求,该请求将获取该保存的图像并将其转发到下一个域,如下所示。我正在使用'request'npm包

 var options = { method: 'POST',
    url: server2 url,
    headers:
        { 
            'cache-control': 'no-cache',
            'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
    formData:
        { 
            filename:
                { value: fs.createReadStream(req.file.path),
                    options: { filename: req.file.path, contentType: null } } } };

request(options, function (error, response, body) {
    if (error) throw new Error(error);


});