Reputation: 181
I want to create get
, post
, put
of the function in electron to access the local JSON file.
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.
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?
Upvotes: 4
Views: 8858
Reputation: 38171
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);
}
}
Upvotes: 5