как значение input из main .js записать в переменную node app.js const { app,…
- User
как значение input из main .js записать в переменную node app.js const { app, BrowserWindow } = require('electron'); const express = require('express'); const bodyParser = require('body-parser'); const http = require('http'); const { fork } = require('child_process'); const path = require('path'); const exp = express();
let serverProcess = null; let input;
// Подключаем парсер тела запроса exp.use(bodyParser.urlencoded({ extended: true })); exp.use(bodyParser.json());
exp.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'index.html')); });
exp.post('/submit', (req, res) => { if (!serverProcess) {
serverProcess = fork(path.join(__dirname, 'app.js')); input = req.body.fname; console.log(input); } else { serverProcess.kill('SIGINT'); serverProcess = null; } });// Заменяем exp.listen() на server.listen() const port = 8000; const server = http.createServer(exp);
server.listen(port, () => { console.log('Server started at http://localhost:8000'); });
const createWindow = () => { const win = new BrowserWindow({ autoHideMenuBar: true, width: 384, height: 384, titleBarStyle: 'hidden', titleBarOverlay: { color: '#0d3e54', symbolColor: '#fff', height: 27 }, webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, nodeIntegration: true, enableRemoteModule: true, nodeIntegrationInWorker: true } })
win.loadURL('file://' + __dirname + '/index.html');
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { createWindow(); } }); }
app.whenReady().then(() => { createWindow(); });
app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } });
- Assistant
Для этого необходимо передать значение input в аргументы командной строки при запуске файла app.js. Вариант реализации может выглядеть так:
// app.js
const input = process.argv[2]; // получаем значение input из аргументов командной строки
console.log(input); // выводим значение input в консоль
// main.js
serverProcess = fork(path.join(__dirname, 'app.js'), [input]); // передаем значение input в качестве аргумента командной строки