简体   繁体   中英

Read/Write local json file in electron by angular

I want to create get , post , put of the function in electron to access the local JSON file.

  1. Currently, I am using json-server to do this but I need to run the localhost every time separately before I run the electron project.

  2. I used another library called - electron-json-storage . But I always get the fs error.

Is there any way to solve this problem or is there any other useful method to do this?

electron uses chromium and runs on node.js , you can use fs (node.js buildin file system module) directly for operating local files.

For example, you can include fs module as a global variable on window at your index.html (from angular project)

window.fs = require('fs')

And build your file service for get , post and post functions or any others based on API from fs via window.fs .

reading local file for example:

@Injectable()
export class FileService {
  fs: any;
  constructor() {
    // or this.fs = <any>window.fs
    this.fs = (window as any).fs;
  }

  // read file synchronous
  getFile(path: string) {
    // return synchronous filestream
    return this.fs.readFileSync(path);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM