Created
March 25, 2024 14:31
-
-
Save Ephraim-Bryski/1a162a9b8c822b2dd5bfee29e20c312f to your computer and use it in GitHub Desktop.
Electron-File-Access-Test
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
fetch('/load_file', { | |
method: "POST", | |
headers: {'Content-Type': 'application/json'}, | |
body: JSON.stringify({file: "public/example.json"}) | |
}).then(response => response.text()).then(a=>{console.log(a)}) |
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
{"hello": "world"} |
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
<script src="client.js"></script> |
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
const { app, BrowserWindow } = require('electron/main') | |
const express = require('express'); | |
const fs = require('fs'); | |
const PORT = 5500; | |
const USE_ELECTRON = true | |
let win | |
function createWindow () { | |
win = new BrowserWindow({width: 800,height: 600}) | |
win.loadFile('public/index.html') | |
} | |
const express_app = express() | |
express_app.use(express.static('public')) | |
express_app.use(express.json()) | |
express_app.post('/load_file', (req, res) => { | |
fs.readFile(req.body["file"], 'utf8', function(err, data){ | |
res.send(data) | |
}) | |
}) | |
if (USE_ELECTRON) { | |
app.whenReady().then(() => {createWindow()}) | |
app.on('window-all-closed', () => { | |
if (process.platform !== 'darwin') {app.quit()} | |
}) | |
}else{ | |
express_app.listen(PORT, () => { | |
console.log(`Server is running on http://localhost:${PORT}`); | |
}) | |
} |
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
{ | |
"name": "electron-save-files-test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "main.js", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"electron": "^29.1.5" | |
}, | |
"dependencies": { | |
"express": "^4.19.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment