Node.js Express Image Resizer and Converter Web App Using Sharp Library Deployed to Heroku 2020

You are currently viewing Node.js Express Image Resizer and Converter Web App Using Sharp Library Deployed to Heroku 2020





Live Demo


You can see the live demo of the app right here


Get Started


In order to get started you need to install these dependencies which are listed below


npm init -y


npm i express


npm i multer


npm i nodemon


npm i sharp


npm i image-size



Now make the entry level file index.js and start a simple express server on port 3000 like this





Now we will load the index.html for our user so that it can interact with the application. Copy paste the code inside the index.html file












Creating Uploads Folder Dynamically



Now we will create the folders dynamically where we will store the image +files which will be processed by sharp and then it will be resized and converted to a image file






Here for the very first time the node.js application loads it will check the condition if the folder public doesn’t exist then it will create this folder and also it will create the uploads folder inside the public folder as a subdirectory.




Setting Public Folder as Static



Now in this block of code we will set the public folder as static so that it can store the image files






Loading the BodyParser Middleware



BodyParser middleware is very much necessary for this application as we will processing the form data which is submitted by the user inside the index.html file. So for that you need to copy paste the below code inside index.js






Uploading Image Files to Server Using Multer



Now the comes the most important part of the application we need to first of all upload the images to the uploads folder before we can process them. So for this we will be using a famous library of node called multer to do this task. Copy paste this block of code to upload files inside node.





Resizing Image Files and Conversion Process



Now we will do the fun part of the application after we upload the images files on the server. We will make use of the sharp library to resize and convert images from one format to another. We will get the format which is selected by the user inside the html form and also we will check if the user has provided the custom width and height of the image or not. If they have provided then we resize the image with that dimensions. If they don’t provide it inside the form then we will pass the default dimensions width and height of the image. We will get the dimensions of the image by image-size package.







Deploy to Heroku



Watch the youtube video given above for this task







Leave a Reply