How to read and write file using fs in node

Issue #419

1
2
3
4
5
6
7
8
9
10
11
12
function write(json) {
const data = JSON.stringify(json)
const year = json.date.getFullYear()
const directory = `collected/${slugify(className)}/${year}`

fs.mkdirSync(directory, { recursive: true })
fs.writeFileSync(
`${directory}/${slugify(studentName)}`,
data,
{ overwrite: true }
)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
async function readAll() {
const classes = fs.readdirSync('classes')
classes.forEach((class) => {
const years = fs.readdirSync(`classes/${class}`)
years.forEach((year) => {
const students = fs.readdirSync(`classes/${class}/${year}`)
students.forEach((student) => {
const data = fs.readFileSync(`classes/${class}/${year}/${student})
const json = JSON.parse(data)
})
})
})
}

Comments