Node.js . Returning csv content from an API

A Brief Guide to Moving Your Content

Unsplash. Photo by Nathan da Silva on

Many systems and platforms allow you to import data from a CSV (comma-separated values) file. This is because CSV is one of the most popular human-readable formats, especially in relation to data science.

What usually happens is that you convert the data returned by the API or query into a valid csv file. This includes boilerplate code or manual work. Fortunately, you can avoid all of this.

This is because you can directly use Node.js to generate the CSV as an output, allowing users to download the CSV file. This is an automated and more practical solution.

Let us now learn how to create an API that returns CSV content in Node.js.

You don’t need any additional libraries to return CSV content to the Express server. At the same time, you can adopt csv-generate NPM library to simplify CSV generation.

you can install csv-generate with the following command:

npm instal csv-generate

it’s not necessary; Here, you will see how to produce and return CSV content in Node.js without additional dependencies.

Content-Type The HTTP header is used to specify the media type of the returned content. As mentioned here, the recommended media type for an HTTP request returning CSV is text/csv,

Also you can use Content-Disposition HTTP header to specify that the content returned by the API is an attachment that should be downloaded and saved locally.

You can set these two HTTP headers in Node.js as follows:

To change <YOUR_FILE_NAME> with the name you want to give to the CSV file returned by the API.

Also note that CSV files are often very different. You may need to create files with different delimiters, enclosed quotes, escaping characters, line separators, encodings, etc. note that csv-generate Allows you to customize the output format of your CSV file.

Now, let’s see what the API returning CSV content looks like in Node.js:

For simplicity, the data used to generate the CSV content was hard-coded. You can generate this data in a real-world scenario by executing a query or making an API call.

Then csvData String variables are initialized with the CSV header. This is not mandatory and depends on how you want your output content to appear. Also, the CSV content generated here uses “,” delimiters, but any other valid delimiter will do.

note that join() Javascript function convert automatically null And undefined in values "" empty string. it’s like that null Or missing content is represented in CSV format.

call now /users/getCSV API, and you will get the following content:

name,surname,age
Patricia,Smith,
John,,56
Maria,Brown,37

As you can see, the output generated by the API is valid CSV content. When called in the browser, users.csv The file will be downloaded automatically.

And voila! You just learned how to return CSV content in Node.js.

Returning CSV content from an API is much more efficient than retrieving the desired data and then converting it to the desired output. You can return a CSV file directly with Node.js. In this article, you learned how to achieve such a result in Express Server.

Thanks for reading! I hope you found this article helpful. Feel free to leave any questions, comments or suggestions.

Leave a Comment