Created
October 5, 2018 18:20
-
-
Save nanohop/df628a6380b9f9f7bd2cdd34f47a3db0 to your computer and use it in GitHub Desktop.
Example of reading a file in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
year | quarter | sales | |
---|---|---|---|
2017 | 1 | 110 | |
2017 | 2 | 115 | |
2017 | 3 | 150 | |
2017 | 4 | 175 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
// Async: | |
fs.readFile('data.csv', 'utf8', (err, data) => { | |
console.log(data) | |
}) | |
// Sync: | |
let results | |
try { | |
// (invalid file error example) | |
const data = fs.readFileSync('nofile.csv', 'utf8') | |
results = data | |
} catch(e) { | |
console.log("error", e) | |
} | |
console.log("results", results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment