<?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>DEV Community: Daniel Adesanya</title>
    <description>The latest articles on DEV Community by Daniel Adesanya (@4x3l3r8).</description>
    <link>https://dev.to/4x3l3r8</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%2F489907%2Fa168aa6f-e99b-4aab-a5ac-1907dd779a58.jpg</url>
      <title>DEV Community: Daniel Adesanya</title>
      <link>https://dev.to/4x3l3r8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/4x3l3r8"/>
    <language>en</language>
    <item>
      <title>Uploading files to image server using ImageKit and Multer in MERN stack</title>
      <dc:creator>Daniel Adesanya</dc:creator>
      <pubDate>Fri, 07 Oct 2022 12:34:55 +0000</pubDate>
      <link>https://dev.to/4x3l3r8/uploading-files-to-image-server-using-imagekit-and-multer-in-mern-stack-4caa</link>
      <guid>https://dev.to/4x3l3r8/uploading-files-to-image-server-using-imagekit-and-multer-in-mern-stack-4caa</guid>
      <description>&lt;p&gt;So, you're a fair beginner in backend development and it's common knowledge that hosting images on your server can be pretty dangerous and annoying to your end users especially when your project/solution is content-intensive (e.g. social app, e-commerce etc.) because it'll cause it to load slowly. Your solution? Giving the content heavy-lifting to dedicated server.&lt;/p&gt;

&lt;p&gt;Well, this was my case as I already had a functioning REST API for my social app built with ExpressJS on NodeJS.&lt;/p&gt;

&lt;p&gt;We'll be using Multer to handle file uploads on our server and &lt;a href="https://imagekit.io" rel="noopener noreferrer"&gt;imagekit&lt;/a&gt; will do all our media heavy lifting. I chose these tools because I just found them easier to use and the latter has a very elaborate documentation (and a free tier too 😋).&lt;/p&gt;

&lt;p&gt;My approach to solving this since I already had my tools were to upload to the file to the imageKit server then get the URL of the uploaded media and then save it to my DB.&lt;/p&gt;



&lt;h3&gt;&lt;b&gt;Let's get to work&lt;/b&gt;&lt;/h3&gt;

&lt;p&gt;We'll start by setting up an account on the &lt;a href="https://imagekit.io" rel="noopener noreferrer"&gt;imagekit&lt;/a&gt; website and then going ahead to install the necessary libraries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`npm i imagekit multer`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we'll need to get some credentials from our imageKit account i.e., urlEndPoint, publicKey and privateKey(this is needed because we'll be doing server-side upload). The keys can be gotten from our imageKit &lt;a href="https://imagekit.io/dashboard/developer/api-keys" rel="noopener noreferrer"&gt;account&lt;/a&gt; and the default urlEndPoint is usually something like this: &lt;code&gt;https://ik.imagekit.io/{your_imageKit_ID}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's grab all of that and put it in our &lt;code&gt;'.env'&lt;/code&gt; file (that's what I did for security's sake), so we have something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IMAGEKIT_PUBLIC_KEY="your_public_key_here"
IMAGEKIT_PRIVATE_KEY="your_private_key_here"
IMAGEKIT_URL_ENDPOINT="your_urlEndPoint_here"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we initialize our imageKit library (I did this in a separate file) with our details gotten from our &lt;code&gt;'.env'&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ImageKit = require("imagekit")
require("dotenv").config()

const imageKit = new ImageKit({
    publicKey: process.env.IMAGEKIT_PUBLIC_KEY,
    privateKey: process.env.IMAGEKIT_PRIVATE_KEY,
    urlEndpoint: process.env.IMAGEKIT_URL_ENDPOINT,
})

module.exports = imageKit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have all of that out of the way let's set Multer up!&lt;/p&gt;

&lt;p&gt;Multer handles file uploads to our server, depending on how you set it, by appending a &lt;code&gt;'file'&lt;/code&gt; property to our API's &lt;code&gt;'req'&lt;/code&gt; object (pretty much like a middleware). So, to set it up we have to initialize its storage type like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Multer = require("multer");

const storage = Multer.memoryStorage()

const multer = Multer({ storage })

module.exports = multer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multer has two basic types of storage which is the disk storage and the memory storage like we're using since we won't be storing the files on our server (which is entirely what we're trying to avoid.). Also notice that the initiated Multer setup is exported too(I just like to think long-term maintenance).&lt;/p&gt;

&lt;p&gt;To use our &lt;code&gt;'multer'&lt;/code&gt; instance, we will import it in the routes of the API we want to use it in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//express router
const router = require('express').Router();

// our example controller(creating posts)
const postController = require('../controllers/postController');

// our multer instance
const multer = require("_pathToMulterInstance_");

router.post("/create", upload.single("file"), postController.createPost);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multer has some functions used to determine what type of file(s) to expect from the API request. Here we have used the &lt;code&gt;'multer.single("file")'&lt;/code&gt; function to let multer know that we're expecting a single file. (The full multer documentation can be found &lt;a href="https://github.com/expressjs/multer" rel="noopener noreferrer"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;center&gt;Now for the Fun part, THE LOGIC!!!&lt;/center&gt;

&lt;p&gt;We move to where we have our controller &lt;/p&gt;

&lt;p&gt;If we send a request to our API now with a file, we should see our file details in the &lt;code&gt;'req'&lt;/code&gt; object. &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%2Fr0walnbxlqwj1ndox7tx.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%2Fr0walnbxlqwj1ndox7tx.jpg" alt="showing the details of the file object in debug mode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note the &lt;code&gt;'buffer'&lt;/code&gt; property of the file object? this is there because of the type of storage we set while setting up multer. It would've been something different (or even absent) if we had used disk storage. &lt;/p&gt;

&lt;p&gt;We should have our imageKit object instance imported into the file we have our controller 'cause this is where we would be using it. We would be using the &lt;code&gt;'upload()'&lt;/code&gt; function of the imageKit class which takes two parameters: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The upload options which requires two necessary properties &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;file: the file to be uploaded(base64)&lt;/li&gt;
&lt;li&gt;file: the name to give the uploaded file(string)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The callback function to trigger when the file upload is done which carries two parameters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;error: if there is any and null if there isn't&lt;/li&gt;
&lt;li&gt;response: which carries the whole necessary information of the file uploaded.
&lt;em&gt;(full imageKit docs can be found &lt;a href="https://docs.imagekit.io/" rel="noopener noreferrer"&gt;here&lt;/a&gt;)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For now, we'll only need the url from the image response and I'm sure you already know how this ends.&lt;/p&gt;

&lt;p&gt;The image below shows the full implementation of the ImageKit file Upload for my scenario (creating posts with images so I just send the image URL with the other post details to the backend).&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%2Fiwa8h8ya5vz8z2zmo02s.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%2Fiwa8h8ya5vz8z2zmo02s.png" alt="final implementation image in the controller"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above, I made a checking to see if there are images and uploads just like that if there are no images. In the upload Options object, I specified the folder which the images uploaded from this API be saved to on the image server. It can be set differently based on your own settings. After necessary processing and saving, it sends a response of the new post which will now include the url of the image that has been saved in the database.&lt;/p&gt;

&lt;p&gt;End Note: You can take the viewing of the images further the the imageKit frontend libraries because it'll give total control over how the images are viewed and scaled.&lt;/p&gt;

&lt;p&gt;Finally, I'd like to know what you would've done differently and any other of your thoughts on this.&lt;/p&gt;

&lt;p&gt;STAY CURIOUS!!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>I quit!...Or should I?</title>
      <dc:creator>Daniel Adesanya</dc:creator>
      <pubDate>Mon, 03 Oct 2022 23:09:06 +0000</pubDate>
      <link>https://dev.to/4x3l3r8/i-quitor-should-i-6d8</link>
      <guid>https://dev.to/4x3l3r8/i-quitor-should-i-6d8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;to future readers(incase anyone even finds this): this is obviously my first post and I'm clearly venting/planning out my next line of action so get what you can and/or just enjoy this piece&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;=====================================================================================&lt;/p&gt;

&lt;p&gt;It's few minutes past 11pm and the day hasn't been so good. I was glued to my screens both phone and laptop, switching between apps LinkedIn, Twitter, Facebook, Gmail hoping to come across or receive a news that'll make me feel very happy. Either a job or some sort of invitation to build something really cool.&lt;/p&gt;

&lt;p&gt;I work on a daily basis sometimes from home, sometimes from the office but it has become quite stale. Same old process, same project, same technologies. I saw this as a problem but how do I fix this? I have to leave. How then do I make money to live? Get a job. How? I could either freelance or work for a more fun organization outside the country of course 'cause we got to factor in gaining completely new experience while earning the big bucks (that's why most of us are in this field, right? Not me though! Y'all be safe).&lt;/p&gt;

&lt;p&gt;Well it's not been a smooth ride either way. I was told with my number of years of experience, it'd be difficult (not impossible though). Really, I feel the No. of years of experience shouldn't really count because what if someone started as a Junior/Intern in a startup and had a shit ton load of work and experience throughout the person's 1-year there and another person got a job to only maintain or build minor features for an already existing product. Crazy Right?!...Well, I've gotten in at least more than 5 Tests and interviews altogether. Not good numbers honestly but I'm staying hopeful.&lt;/p&gt;

&lt;p&gt;Since I have identified this as a primary problem, I need to get to working on solving it. How?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start writing&lt;/strong&gt; : I have a very interesting mind but I don't interact much with people. It'd be nice to build a community  of some sort of people with likewise controversial thoughts especially about tech.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reach out to more people&lt;/strong&gt; : This pains me to write cause it's almost just too hard for me to randomly reach out to people(Strangers!! Especially when our parent has told us not to talk to them😪)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Build more useful real-life projects&lt;/strong&gt; : This is probably the most easiest on this list cause I do this practically everyday. Turns out Coding is only but a small fraction of the actual work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Building a social presence(LinkedIn, Twitter)&lt;/strong&gt; : Probably not that important but it'll  create awareness on what I do and keep my DMs flowing with Jobs and opportunities(Or so they say).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GOD FIRST&lt;/strong&gt; : The most important to me, I guess. I may not be too spiritual but I damn well believe God is real and he works wonders so it'd be foolish to depend solely on my works and not bring him in.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is my plan to turn a new leaf and not quit until I get a new and interesting offer. Hopefully, it works.&lt;/p&gt;

&lt;p&gt;STAY CURIOUS!!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>devjournal</category>
      <category>management</category>
    </item>
  </channel>
</rss>
