Мы создали веб-сервер, используя Express для приложения Electron, но производительность GET-запроса очень низкая! Любая конкретная c причина этого?
вы можете скачать приложение и исходный код здесь http://openspeedtest.com/speed-testing-application-for-your-website.php#Option -3
Но почтовый запрос выполняется очень хорошо.
Индекс. js
const { app, BrowserWindow } = require('electron');
const express = require('express');
const path = require('path');
const cors = require('cors');
const internalIp = require('internal-ip');
const tcpPortUsed = require('tcp-port-used');
const ExpressApp = express();
global.ExpressApp = ExpressApp;
ExpressApp.use(cors());
ExpressApp.use('/', express.static(__dirname + '/public'));
global.ExpressApp = ExpressApp;
ExpressApp.post('/upload', (req, res) => {
req.body = null;
res.sendStatus(200);
});
//npm install
//npx create-electron-app openspeedtest-server
//npm start
//npm run make
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit();
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
});
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, 'index.html'));
// Open the DevTools.
//mainWindow.webContents.openDevTools();
};
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
//app.on('ready', createWindow);
app.on('ready', function () {
var IPadd = internalIp.v4.sync();
var Port = 3000;
//console.log(internalIp.v6.sync())
//tcpPortUsed.check(Port, IPadd)
getPort = function (Port) {
tcpPortUsed.check(Port, IPadd)
.then(function (inUse) {
console.log('Port ' + Port + " in use = " + inUse);
if (inUse) {
Port = Port + 1;
console.log('Trying new port = ' + Port)
getPort(Port)
//console.error('Error on check:', err.message);
} else {
console.log(Port + " is Free!")
var ExpressAppServer = ExpressApp.listen(Port, function () {
global.ExpressAppServer = ExpressAppServer;
//var port = ExpressAppServer.address().port;
console.log('OpenSpeedTest-Server is running..', IPadd, ':', Port);
createWindow();
});
}
}, function (err) {
});
}
getPort(Port)
// var ExpressAppServer = ExpressApp.listen(80, function () {
//global.ExpressAppServer = ExpressAppServer;
// var port = ExpressAppServer.address().port;
//console.log('App now running on ', IPadd, ':', port);
//createWindow();
//});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
//BrowserWindow({ icon: 'src/icon.png' });
createWindow();
}
});
индекс. html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OpenSpeedTest-Server Ver:1.0</title>
<style>
body {
background: -webkit-gradient(linear, left top, left bottom, from(#56c4fb), to(#0baeff)) fixed;
}
.button {
background-color: #e7e7e7;
/* Green */
border: none;
color: black;
padding: 10px 22px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 15px;
}
.port {
border: 9px #008CBA;
border-radius: 15px;
width: 50px;
height: 50px;
}
</style>
</head>
<body>
<h1>OpenSpeedtest-Server Ver:1.0</h1></br></br>
<h1>Network Speed Test Server Status : Running...</h1></br></br>
<h1>
<div id=ipadder></div>
</h1></br></br>
<h1>Change Port If Needed.</h1></br></br>
<input class="port" type="number" id="ChangePort"></input>
<button class="button" type="submit" id="restart">Update & Restart Server</button>
<script>
window.onload = function () {
let expressServer = require('electron').remote.getGlobal('ExpressAppServer');
const express = require('electron').remote.getGlobal('ExpressApp');
const internalIp = require('internal-ip');
//const cors = require('cors').remote.getGlobal('cors');
var port = expressServer.address().port;
console.log(internalIp.v4.sync() + " " + port)
var ipadder = document.getElementById('ipadder');
ipadder.innerHTML = ("Please visit this url : " + 'http://' + internalIp.v4.sync() + ":" + port)
document.getElementById("restart").addEventListener("click", function () {
var newPort = document.getElementById("ChangePort").value;
expressServer.close();
expressServer = express.listen(newPort, function () {
var port = expressServer.address().port;
ipadder.innerHTML = ("Please visit this url : " + 'http://' + internalIp.v4.sync() + ":" + port)
//console.log('App now running on port', port);
});
});
}
</script>
</body>
</html>
пакет. json
{
"name": "Openspeedtest-server",
"productName": "Openspeedtest-server",
"version": "1.0.0",
"description": "Network Speed Test Server - by OpenSpeedTest",
"main": "src/index.js",
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "echo \"No linting configured\""
},
"keywords": [],
"author": "OpenSpeedTest",
"license": "MIT",
"config": {
"forge": {
"packagerConfig": {
"icon": "src/icon.icns"
},
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "openspeedtest_server",
"icon": "src/icon.ico",
"setupIcon": "src/icon.ico"
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
]
},
{
"name": "@electron-forge/maker-deb",
"config": {
"icon": "src/1024x1024.png"
}
}
]
}
},
"dependencies": {
"cors": "^2.8.5",
"electron-squirrel-startup": "^1.0.0",
"express": "^4.17.1",
"internal-ip": "^6.0.0",
"path": "^0.12.7",
"tcp-port-used": "^1.0.1"
},
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.52",
"@electron-forge/maker-deb": "^6.0.0-beta.52",
"@electron-forge/maker-rpm": "^6.0.0-beta.52",
"@electron-forge/maker-squirrel": "^6.0.0-beta.52",
"@electron-forge/maker-zip": "^6.0.0-beta.52",
"electron": "^9.1.0"
}
}