1

Using createReadStream(), I'm attempting to stream a CSV file stored in my program's directory, however, it seems to be returning a buffer with a string of the file's bytes, as opposed to the actual text content. I was of the impression that readFile() was used for buffering files, while createReadStream() would stream the file. Perhaps I'm just ignorant to the way the streams truly work.

At this point, I'm simply outputting the data to see what the program is streaming. Code is as follows:

const fs = require("fs");
const path = require("path");

// These variables set the filepath of the CSV file
const basePath = "./"
const fileName = "dummy.csv"

// Create the path string
const fileLoc = path.join(basePath, fileName);
console.log(fileLoc);

fs.createReadStream(fileLoc).on("data", (data) => { console.log(data); })

Output:

<Buffer 54 49 54 4c 45 2c 20 41 52 54 49 53 54 2c 20 59 45 41 52 0d 0a 43 61 6e 20 59 6f 75 20 46 65 65 6c 20 4d 79 20 48 65 61 72 74 2c 20 42 72 69 6e 67 20 ... 122 more bytes>

Note that I haven't piped the data - this is because I can't think of what writable stream to pipe the data to, or what I would need to do to create one. My issue may well lie here.

Thanks in advance

5
  • See a bunch of pre-built modules for streaming csv files: google.com/search?q=npm+csv+stream. If you want to see how they work, just go look at their source code. Commented Dec 30, 2019 at 9:56
  • 3
    FYI, you can convert a buffer to a UTF-8 stream with buf.toString(...). Commented Dec 30, 2019 at 10:03
  • @jfriend00 wow, such a simple solution. Thank you, looks like it's time to educate myself on how buffers work Commented Dec 30, 2019 at 10:06
  • 1
    Here on stackoverflow, it is NOT the proper procedure to mark your question SOLVED in the title or to put the solution in your question. That is not how things work here. Questions are only for questions, not answers. Answers go in answers. You can even post an answer to your own question if you want. Please edit your question to remove the solution and "solved" stuff. I've posted an answer that you can accept by clicking the checkmark to the left of it to indicate to the community that your question has been answered and is done. That's the proper procedure here. Commented Dec 30, 2019 at 17:04
  • @jfriend00 apologies, will do Commented Dec 31, 2019 at 10:50

1 Answer 1

1

You can convert your buffer to a UTF-8 string with buf.toString(...).

You can also just use a pre-built module for streaming CSV files. There are plenty of them here on NPM. And, you can look at the source code for any of those modules if you want to see how they work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.