покажи как сервер должен принимать значение input main.js const { app, BrowserWindow } =…
- User
покажи как сервер должен принимать значение input main.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 main.js, необходимо добавить обработчик POST запроса на сервере, который будет принимать данные из формы на странице и передавать их в переменную input, которая была объявлена ранее.
Пример кода обработчика запроса:
exp.post('/submit', (req, res) => { // присваиваем значение переменной input из тела запроса input = req.body.inputValue; // выводим значение переменной input в консоль console.log(
Input value: ${input}); // отправляем ответ клиенту res.send('Input value received'); });Где 'inputValue' - это имя поля ввода в HTML форме, из которого будут браться данные. Общий код сервера останется таким же, как и в примере выше.