ItSolutionStuff.com

How to Download Image from URL in Node JS?

By Hardik Savani • September 1, 2021
Node JS

Today, i will let you know example of node js download image from url. We will look at example of how to download image from url in node js. if you want to see example of node js get image from url then you are a right place. i explained simply about node js download file from url.

I will give you very simple example of how to download image or file from url in node.js app. let's see bellow example code:

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

server.js

var fs = require('fs'),

http = require('http'),

https = require('https');

var Stream = require('stream').Transform;

var downloadImageFromURL = (url, filename, callback) => {

var client = http;

if (url.toString().indexOf("https") === 0){

client = https;

}

client.request(url, function(response) {

var data = new Stream();

response.on('data', function(chunk) {

data.push(chunk);

});

response.on('end', function() {

fs.writeFileSync(filename, data.read());

});

}).end();

};

downloadImageFromURL('https://www.itsolutionstuff.com/assets/images/logo-it.png', 'it.png');

now you can simply run by following command:

node server.js

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Node JS Send Email with Attachment Example

Read Now →

How to Send Email using Gmail Account in Node.js?

Read Now →

How to Get Current Date and Time in Node JS?

Read Now →

How to Get Client IP Address in Node JS?

Read Now →

How to Render HTML File in Node JS Express?

Read Now →

How to Create Separate Routes File in Node JS Express?

Read Now →

How to Get Query String Value in Node.js?

Read Now →

Node JS Express Route with Parameters Example

Read Now →

How to Get Data from Json File in Node JS?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

Node JS Foreach Loop Array Example

Read Now →