<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: David Okonji</title>
    <description>The latest articles on Forem by David Okonji (@davidokonji).</description>
    <link>https://forem.com/davidokonji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F171882%2F8363c54e-c6df-4ab2-ad24-27b7027ec0fb.jpeg</url>
      <title>Forem: David Okonji</title>
      <link>https://forem.com/davidokonji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/davidokonji"/>
    <language>en</language>
    <item>
      <title>Remote Debugging for Android Devices</title>
      <dc:creator>David Okonji</dc:creator>
      <pubDate>Wed, 20 Apr 2022 08:03:47 +0000</pubDate>
      <link>https://forem.com/davidokonji/remote-debugging-for-android-devices-325a</link>
      <guid>https://forem.com/davidokonji/remote-debugging-for-android-devices-325a</guid>
      <description>&lt;p&gt;While working on android applications development, it is a common practice to debug apps using a USB cable to connect the device to android studio. With the decreasing number of computer's ports, for which we require an external dongle device to connect USB cables. By using remote debugging, it saves a lot of time and space needed to carry external accessories used to debug a mobile application on an android device.&lt;/p&gt;

&lt;p&gt;A couple years back I wrote an article on &lt;a href="https://dev.to/davidokonji/remote-debugging-in-react-native-3o72"&gt;&lt;strong&gt;Remote Debugging in React Native&lt;/strong&gt;&lt;/a&gt; which was majorly focused on using Xcode to connect to IOS devices remotely, at that time remote debugging option was not available for android studio.&lt;/p&gt;

&lt;p&gt;In this article, I will be explaining how to set up remote debugging for an android application over the network using Android Studio and an Android device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is not limited to testing a native android application but also for react native and flutter applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Required Tools
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/studio" rel="noopener noreferrer"&gt;Android Studio Bumblebee | 2021.1.1 Patch 3&lt;/a&gt; or later&lt;/li&gt;
&lt;li&gt;A android device running on Android 11+&lt;/li&gt;
&lt;li&gt;A wireless connection common to both mobile phone and system running the android studio&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Steps to setup
&lt;/h3&gt;

&lt;p&gt;Now, we have all tools setup, we can go ahead to open our android project in android studio and follow the steps below;&lt;/p&gt;

&lt;h4&gt;
  
  
  On the android test device
&lt;/h4&gt;

&lt;p&gt;We should ensure the &lt;strong&gt;Developer Options&lt;/strong&gt; is enabled, In developer options, an option for &lt;strong&gt;Wireless debugging&lt;/strong&gt; should be toggled off by default, it should be toggled on&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8v2msrqznk4gy2ftjsy.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8v2msrqznk4gy2ftjsy.jpg" alt="Android wireless debugging on"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tapping on the enabled wireless debugging should get you options to connect wirelessly&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pffti0pyxojsxizgw75.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1pffti0pyxojsxizgw75.jpg" alt="Android wireless debugging options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  In android studio
&lt;/h4&gt;

&lt;p&gt;We should click on the dropdown to select a device at the top right menu bar,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx26m9y6o2sazpctzzcxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx26m9y6o2sazpctzzcxb.png" alt="Android Device selection"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That should give a pop up dialog which give a default option to &lt;strong&gt;Pair using QR code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;-&amp;gt; &lt;strong&gt;Pairing using QR code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2b7efv53nom8jzjrcv9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2b7efv53nom8jzjrcv9.png" alt="Pairing using QR code"&gt;&lt;/a&gt;&lt;br&gt;
On the mobile device already been setup above, we click on the option to &lt;code&gt;Pair device with QR code&lt;/code&gt; which gives a QR scanner screen to scan the QR generated by android studio&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ewg4s87ea1w9ifbbhv8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ewg4s87ea1w9ifbbhv8.jpg" alt="Scan Android QR"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once scanned correctly, we get a successfully paired status and would see the new paired devices under the list&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98neslb7soyc9r1uf4ol.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98neslb7soyc9r1uf4ol.jpg" alt="Successfully paired"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-&amp;gt; &lt;strong&gt;Pairing using pairing code&lt;/strong&gt;&lt;br&gt;
On the mobile device already been setup above, we click on the option to &lt;code&gt;Pair device with pairing code&lt;/code&gt; to generate a pairing code which would be used to finish the pairing set up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqhp9tclk160wkhslqj4m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqhp9tclk160wkhslqj4m.jpg" alt="Pairing using pairing code start"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In android studio we should toggle the &lt;code&gt;Pair using pairing code&lt;/code&gt; option in the popup dialog, we should see our device ready to start pairing&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6z7rjoxgtb4qucpje3nk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6z7rjoxgtb4qucpje3nk.png" alt="Start Pairing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Input the code generated on the mobile phone into the fields on android studio&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi2olmv3t86y7uyvnrx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi2olmv3t86y7uyvnrx1.png" alt="Code input"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once scanned correctly, we get a successfully paired status and would see the new paired devices under the list&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4c2mw692ra0jsl4tpx83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4c2mw692ra0jsl4tpx83.png" alt="Successful"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Either ways we go about pairing the device, once done we should see our device available for the app to run on anytime we are connected to the same network 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb75xlrhp2tlf6wljznu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb75xlrhp2tlf6wljznu.png" alt="Run on device"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Can't see developer options? How to toggle developer options on a new device:&lt;/strong&gt;
Go to phone settings -&amp;gt; Scroll to the bottom and click on the about phone option -&amp;gt; Click on software information -&amp;gt; Tap on the &lt;code&gt;Build number&lt;/code&gt; 7 times to get a prompt that developer options has been enabled.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Can't see device to pair with?&lt;/strong&gt;
Confirm if the android device and system running the android studio are on the connected to same network.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Using remote debugging generally saves a lot of time to debug mobile applications, Hopefully it would save yours.&lt;/p&gt;

&lt;p&gt;Thanks for reading 🥳&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>ios</category>
      <category>android</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Remote Debugging in React Native</title>
      <dc:creator>David Okonji</dc:creator>
      <pubDate>Mon, 21 Dec 2020 13:06:43 +0000</pubDate>
      <link>https://forem.com/davidokonji/remote-debugging-in-react-native-3o72</link>
      <guid>https://forem.com/davidokonji/remote-debugging-in-react-native-3o72</guid>
      <description>&lt;p&gt;Coming from an Expo background in React Native development, remote debugging is an out of the box solution, as the Expo client provides one with the option of debugging react native applications over the network.&lt;/p&gt;

&lt;p&gt;In this article, I would be explaining how to set up remote debugging for a react native application over a local network using Xcode and an IOS device.&lt;/p&gt;

&lt;p&gt;With &lt;a href="https://apps.apple.com/ng/app/xcode/id497799835?mt=12" rel="noopener noreferrer"&gt;Xcode&lt;/a&gt; already installed and set up on your system, we would follow the steps highlighted below, In this article Xcode &lt;em&gt;version 12.3&lt;/em&gt; is being used.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; for the very first time you would need a cable to be connected to the phone you intend to use to test.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step by Step guide
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Ensure the system and the device which needs to be used to test are connected to the same Wi-Fi.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open Xcode, proceed to the &lt;strong&gt;window&lt;/strong&gt; menu option, select the &lt;strong&gt;Device and simulators&lt;/strong&gt; option.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkpzq6s0vrr8ykp51z0ad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkpzq6s0vrr8ykp51z0ad.png" alt="Device and simulators"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On selecting the option, a popup window comes up showing your existing simulators and device.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3h4k6oqtvjlaqi3appl7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3h4k6oqtvjlaqi3appl7.png" alt="Device and simulators dialog"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;Devices&lt;/strong&gt; tab to see the connected devices to see your connected phone&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0bsg3yo903af8yp4c7ld.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0bsg3yo903af8yp4c7ld.png" alt="connect to network image"&gt;&lt;/a&gt;&lt;br&gt;
Select the &lt;strong&gt;Connect via network&lt;/strong&gt; checkbox and you are good to go. You can now unplug the phone from the system and test your apps over the network.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Debugging Issues
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In a case where the device is not showing as &lt;strong&gt;connected&lt;/strong&gt; when plugged in with a cable and does not run regularly when connected, you may need to update the version of Xcode to match the updates for the device.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In a case where the &lt;strong&gt;connect via network&lt;/strong&gt; option does not come up, please check your connection and ensure the device and the system are connected to the same network, and try again.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To setup remote debugging for android devices a good guide to follow &lt;a href="https://developers.google.com/web/tools/chrome-devtools/remote-debugging" rel="noopener noreferrer"&gt;Get Started with Remote Debugging Android Devices&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading this far 😃. Feedback is greatly appreciated.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Generating and Downloading CSV files using express JS</title>
      <dc:creator>David Okonji</dc:creator>
      <pubDate>Mon, 18 May 2020 11:38:54 +0000</pubDate>
      <link>https://forem.com/davidokonji/generating-and-downloading-csv-files-using-express-js-1o4i</link>
      <guid>https://forem.com/davidokonji/generating-and-downloading-csv-files-using-express-js-1o4i</guid>
      <description>&lt;p&gt;While working on a node JS project, I had the need to pull certain data from my database and represent values in a CSV file, This guide aims at simplifying the process of creating a CSV file and making it downloadable from the client-side.&lt;/p&gt;

&lt;p&gt;Prerequisites&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Knowledge using express JS.&lt;/li&gt;
&lt;li&gt;A working database setup.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this tutorial, I would be making use of PostgreSQL with Sequelize ORM (database/ORM type does not actually matter), &lt;a href="https://www.npmjs.com/package/json2csv"&gt;&lt;code&gt;json2Csv&lt;/code&gt;&lt;/a&gt; npm package is my choice package for parsing JSON to CSV data because of its simplicity and ease of use. other npm packages which can be used are &lt;a href="https://www.npmjs.com/package/fast-csv"&gt;&lt;code&gt;fast-csv&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://www.npmjs.com/package/csv-parse"&gt;&lt;code&gt;csv-parse&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objective
&lt;/h3&gt;

&lt;p&gt;Downloading users data (first name, last name, email) as a CSV file, using an API endpoint &lt;code&gt;/download&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;utility function&lt;/li&gt;
&lt;li&gt;Using the helper function in the controller&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Utility Function
&lt;/h4&gt;

&lt;p&gt;Install the package using &lt;code&gt;yarn add json2csv&lt;/code&gt; or &lt;code&gt;npm i json2csv&lt;/code&gt;, once complete, we would make use of the parser synchronous API that is giving from &lt;a href="https://www.npmjs.com/package/json2csv"&gt;&lt;code&gt;json2Csv&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;util.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Parser&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;json2csv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;downloadResource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;json2csv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Parser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;json2csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/csv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attachment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The downloadResource function parameters;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;res&lt;/strong&gt;: This is a response from the API endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fileName&lt;/strong&gt;: The name intended for the generated CSV file to be downloaded.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fields&lt;/strong&gt;: This is an array of objects that has a number of functionalities which includes, specifying what values should be parsed from data sent, giving a custom header to each column etc.
&lt;strong&gt;Note&lt;/strong&gt;: For this tutorial we are going to supply fields, but fields are optional, when defining the parser instance it can accept an empty parameter (i.e &lt;code&gt;new Parser()&lt;/code&gt;). The keys (i.e &lt;code&gt;label&lt;/code&gt;, &lt;code&gt;value&lt;/code&gt;) of the objects would be used to give structure to the CSV file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;data&lt;/strong&gt;: This is an array of objects representing data to be populated in the CSV file, &lt;strong&gt;Note&lt;/strong&gt;: this is to match the specified field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the utility function, set the header &lt;code&gt;Content-type&lt;/code&gt; to &lt;code&gt;text/csv&lt;/code&gt; to enable the client to recognize what type of data is being sent and how to handle it, setting the &lt;code&gt;attachment&lt;/code&gt; to the file name of choice and then finally returning a response using &lt;code&gt;res.send&lt;/code&gt; taking the parsed CSV data as parameters. We have our ready to use utility function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using the helper function in the controller
&lt;/h4&gt;

&lt;p&gt;Creating a new route for the download&lt;br&gt;
&lt;em&gt;route.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;userController&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./controllers/userController&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

 &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/download&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="c1"&gt;// rest of express configs&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating a new controller download method&lt;br&gt;
&lt;em&gt;userController.js&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../models&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;downloadResource&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../util&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

 &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;download&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;First Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Last Name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;last_name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Email Address&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;email_address&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;downloadResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users.csv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;

 &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Testing Implementation
&lt;/h4&gt;

&lt;p&gt;Run your local server and open link in the client browser with a path for the download endpoint (e.g &lt;code&gt;http://localhost:3000/api/download&lt;/code&gt;) and your file gets downloaded using your local download manager.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Fun fact this is my first article on dev.to 🎉, I am David Okonji a software engineer based in Lagos, Nigeria, feedback would be greatly appreciated. Gracias!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>csv</category>
      <category>node</category>
    </item>
  </channel>
</rss>
