Welcome folks today in this blog post we will be downloading csv
files from url
using request pipe()
stream method. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the request
module using the npm command as shown below
npm i request
After that you need to make an index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 |
const request = require('request') const fs = require('fs') const url = "https://peoplefssdf.sc.fsu.edu/~jburkardt/data/csv/faithful.csv" const file = fs.createWriteStream('output.csv') |
As you can see we are importing the request
module at the very top and then we are declaring the csv
file url where we will be downloading it and saving it inside the local disk. And then we are declaring the output
filename using the createWriteStream()
of the fs
module.
1 2 3 4 5 6 7 8 |
request(url) .pipe(file) .on('finish',() => { console.log("file is successfully downloaded") }) .on('error',() => { console.log("file url is incorrect") }) |
As you can see we are using the request()
module and then passing the url
inside it and then we are using the pipe()
method to download the csv
file stream from the url. And then we are listening on for various events such as finish
and error
events.
node index.js