Moh .S
Moh .S

Reputation: 2090

How to download contents from url to a file in node js

I have a json content on a particular URL. I want to download that json content to a file say test.json.

Upvotes: 1

Views: 1700

Answers (2)

Adam
Adam

Reputation: 5253

You can use the node-fetch module:

var fetch = require('node-fetch');
fetch('https://example.com/file.json')
    .then(function(response) {
        var destination = fs.createWriteStream('./destination-file.json');
        response.body.pipe(destination);
    });

Upvotes: 1

Moh .S
Moh .S

Reputation: 2090

Found the answer myself,

I used the "packages" request and "fs"

var request = require("request");
var fs = require("fs");

 request
 .get(theURLWithJson)
 .on('error', function(err) {
    console.log('err', err);
  })
 .pipe(fs.createWriteStream(fullFilePathWithFileName));

Upvotes: 0

Related Questions