How to Pass ( upload ) a file from one API to another API in Nodejs?

Barathirajan M
Technology Specialist
February 28, 2019
Rate this article
[Total: 1    Average: 5/5]
Views    641

We all know how to upload a file from client(Web) to server. In this article we are gonna see how to upload a file from one server to another server in NodeJS. Initially we tried to pipe the incoming multipart form data request directly to other server, Since we lost some data while doing so,  we switched to the following methodology.

Assume both  the servers we are dealing with uses the REST API with multipart form data for handling files.

Steps to Follow To Upload File:

1. Install the following npm modules

  • request – npm i request – To make a post call to another server which receives the uploaded files
  • multiparty – npm i multiparty– To Parse the file and text from the incoming request
  • fs – built-in library – To read the file from the temp location given by multiparty

2. Use the following code block to import the installed packages

const req = require("request");
const fs = require("fs");
const multiparty = require("multiparty");

3.  Use the following code block to parse the files and text input from the incoming multipart – form data request.
Here form is the object provided by the multiparty package.

let form = new multiparty.Form();
 form.parse(request, function(err, fields, files) {
     console.log(err)
     console.log(fields)
     console.log(files)
 });

4. During the form.on(“file) event, Multiparty Form object gives file details and file location from the incoming request. Use the following  code block to read the file and to create a new multipart form data payload.

form.on('file',function(name, file) {
             let formData = {
                 file: {
                     value: fs.createReadStream(file.path),
                     options: {
                         filename: file.originalFilename
                     }
                 }
             }
});

5. Use Following code block to make a request to another server.

form.on('fileunction(name, file) {
     let formData = {
         file: {
             value: fs.createReadStream(file.path),
             options: {
                 filename: file.originalFilename
             }
         }
     };
const postUrl = "http://example.com"   //replace your upload url here
 
req.post({url: postUrl,formData: formData }, function(err, httpResponse, body) {     
console.log(err);     
console.log(httpResponse);     
console.log(body) 
}); 
console.log(file.path);
 });

6. After a successful upload of a file into another server, Don’t forget to unlink the file from the temporary location. otherwise, it would cause serious memory leakage issues to your application. Use the following code to unlink the file.

fs.unlink(file.path, (_err) => {});

7. Here is the complete code snippet (Handler) that we have discussed so far

uploadfiles(request, response) {

const req = require("request");
const fs = require("fs");
const multiparty = require("multiparty");
let form = new multiparty.Form();

form.parse(request, function(err, fields, files) {
    console.log(err)
    console.log(fields)
    console.log(files)
});
form.on('file', function(name, file) {
    let formData = {
        file: {
            value: fs.createReadStream(file.path),
            options: {
                filename: file.originalFilename
            }
        }
    };
    const postUrl = "http://example.com" //replace your upload url here     req.post({url: postUrl,formData: formData }, function(err, httpResponse, body) {         
    console.log(err);
    console.log(httpResponse);
    console.log(body)
    fs.unlink(file.path, (_err) => {});
});
console.log(file.path);
});
}
}
Subscribe To Our Newsletter

Leave a comment