title: Send data from Node.js back to Client
body: <p>I have an IOS React Native App which uses Node.js program to download photos from Google Cloud bucket. I could get the photos from specific folder but I am not able to send them back to client. Below is the Client Node.js program:</p>
  <p>index.js</p>
  <pre class="lang-js prettyprint-override"><code>const express = require('express')
  const bodyParser = require('body-parser')
  const multer = require('multer')
  const uploadImage = require('./helpers/helpers')
  const downloadImage = require('./helpers/downloadhelpers');
  
  const app = express()
  
  const multerMid = multer({
    storage: multer.memoryStorage(),
    limits: {
      // no larger than 5mb.
      fileSize: 25 * 1024 * 1024,
      //fileSize: maxSize
    },
  })
  
  app.disable('x-powered-by')
  app.use(multerMid.single('file'))
  app.use(bodyParser.json())
  app.use(bodyParser.urlencoded({
    extended: false
  }))
  
  app.post('/uploads', async (req, res, next) =&gt; {
    try {
      const myFile = req.file
      const destination = req.body.folder_name;
  
      const imageUrl = await uploadImage(myFile, destination)
      res
        .status(200)
        .json({
          message: &quot;Upload was successful&quot;,
          data: imageUrl
        })
    } catch (error) {
      next(error)
    }
  })
  
  app.get('/file_download', async (req, res, next) =&gt; {
    const folder_name = req.body.folder_name;
    try {
      const ImageDownload = await downloadImage(folder_name)
      console.log(ImageDownload);
      res
        .status(200)
        .json({
          message: &quot;Download Successful&quot;,
          data: ImageDownload
        })
    } catch (error) {
      next(error)
    }
  })
  
  app.use((err, req, res, next) =&gt; {
    res.status(500).json({
      error: err,
      message: 'Internal server error!',
    })
    next()
  })
  
  app.listen(9001, () =&gt; {
    console.log('app now listening for requests!!!')
  })
  </code></pre>
  <p>Below is the upload node which is working nice as purposed:</p>
  <p>helpers.js</p>
  <pre class="lang-js prettyprint-override"><code>const util = require('util');
  const gc = require('./../config')
  const bucket = gc.bucket('jtg_app_bucket') // should be your bucket name
  
  module.exports = uploadImage = (File, destination) =&gt; new Promise((resolve, reject) =&gt; {
    const {
      originalname,
      buffer
    } = File
    const folder_name = destination;
    console.log(folder_name);
    const blob = bucket.file(destination + '/' + originalname.replace(/ /g, &quot;_&quot;))
    const blobStream = blob.createWriteStream({
      resumable: false
    })
    blobStream.on('finish', () =&gt; {
        const publicUrl = util.format(
          `https://storage.googleapis.com/${bucket.name}/${folder_name.name}/${blob.name}`
        )
        resolve(publicUrl)
      })
      .on('error', () =&gt; {
        reject(`Unable to upload image, something went wrong`)
      })
      .end(buffer)
  })
  </code></pre>
  <p>Below is the download node which sends data after fetch from GCP bucket:</p>
  <p>downloadhelpers.js</p>
  <pre class="lang-js prettyprint-override"><code>module.exports = downloadImage = (FolderToDisplay) =&gt; new Promise((resolve, reject) =&gt; {
    const folder_name = FolderToDisplay;
    const {
      Storage
    } = require('@google-cloud/storage');
    var projectId = '*****************' // Your Google Cloud Platform project ID
    var keyFilename = './../UploadPhotosNode/config/keys.json'
    const storage = new Storage({
      keyFilename,
      projectId
    });
    var bucket = storage.bucket(&quot;gs://jtg_app_bucket&quot;);
    const bucketName = 'jtg_app_bucket';
  
    // The directory prefix to search for
    const prefix = folder_name + '/';
  
    // The delimiter to use
    const delimiter = '/';
    const options = {
      prefix: prefix,
    };
  
    bucket.getFiles(options).then(results =&gt; {
        const files = results[0];
        var resData = {
          success: true,
          images: {
            results: []
          }
        };
        files.forEach(file =&gt; {
          resData.images.results.push({
            uri: 'https://storage.googleapis.com/' + bucketName + '/' +
              file.name
          });
        })
        var stringified = JSON.stringify(resData);
        var only_images = JSON.parse(stringified);
  
        const data = only_images.images.results;
        console.log(data);
        resolve(data)
  
      })
      .on('error', () =&gt; {
        reject(`Unable to download images, something went wrong`)
      })
      .end(buffer)
  })
  </code></pre>
  <p>Below is the data I am getting from the download node program:</p>
  <pre class="lang-json prettyprint-override"><code>[
    { &quot;uri&quot;: &quot;https://storage.googleapis.com/jtg_app_bucket/device_6/image.jpg&quot; },
    { &quot;uri&quot;: &quot;https://storage.googleapis.com/jtg_app_bucket/device_6/loved_it.jpg&quot; }
  ]
  </code></pre>
  <p>I need to send data using resolve(data) like in upload node program and I am not able to do that. Can anyone suggest how to do that? Thanks in advance.</p>
  <p>I am getting the data in const data in console, I want to send that back inside resolve() to /file_download in index.js. In Postman, when I run &quot;http://localhost:9001/file_download&quot;, I am getting error as:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;error&quot;: {},
    &quot;message&quot;: &quot;Internal server error!&quot;
  }
  </code></pre>
  <p>I want the output as</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;message&quot;: &quot;Download Successful&quot;,
    &quot;data&quot;: [
      { &quot;uri&quot;: &quot;https://storage.googleapis.com/jtg_app_bucket/device_6/image.jpg&quot; },
      { &quot;uri&quot;: &quot;https://storage.googleapis.com/jtg_app_bucket/device_6/loved_it.jpg&quot; }
    ]
  }
  </code></pre>
  <p>So that I can use this to display the images inside React Native App. Hope this is clear.</p>