Created
May 5, 2022 10:12
-
-
Save andreciornavei/c467d844c73ea3905dd9246750afeeae to your computer and use it in GitHub Desktop.
Write and Read Json File on Javascript
This file contains hidden or 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
| 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