Skip to content

Instantly share code, notes, and snippets.

@andreciornavei
Created May 5, 2022 10:12
Show Gist options
  • Select an option

  • Save andreciornavei/c467d844c73ea3905dd9246750afeeae to your computer and use it in GitHub Desktop.

Select an option

Save andreciornavei/c467d844c73ea3905dd9246750afeeae to your computer and use it in GitHub Desktop.
Write and Read Json File on Javascript
import { writeFile, readFile } from "fs/promises"
export const save = async (data) => {
const currentData = await read("./../database.json")
currentData.push(data)
await writeFile(databaseFile, JSON.stringify(currentData))
}
export const read = async (filename) => {
const { pathname: databaseFile } = new URL(filename, import.meta.url)
return JSON.parse((await readFile(databaseFile)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment