Skip to content

Instantly share code, notes, and snippets.

@nanohop
Created October 5, 2018 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanohop/df628a6380b9f9f7bd2cdd34f47a3db0 to your computer and use it in GitHub Desktop.
Save nanohop/df628a6380b9f9f7bd2cdd34f47a3db0 to your computer and use it in GitHub Desktop.
Example of reading a file in node.js
year quarter sales
2017 1 110
2017 2 115
2017 3 150
2017 4 175
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