Skip to content

Instantly share code, notes, and snippets.

@Ephraim-Bryski
Created March 25, 2024 14:31
Show Gist options
  • Save Ephraim-Bryski/1a162a9b8c822b2dd5bfee29e20c312f to your computer and use it in GitHub Desktop.
Save Ephraim-Bryski/1a162a9b8c822b2dd5bfee29e20c312f to your computer and use it in GitHub Desktop.
Electron-File-Access-Test
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)})
{"hello": "world"}
<script src="client.js"></script>
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}`);
})
}
{
"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