electron创建本地服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const { app, BrowserWindow } = require('electron')
const PORT = 8080

// electron main
console.log(process.versions);

app.on("ready", function () {
var mainWindow = new BrowserWindow({ show: true });
mainWindow.loadURL("file://" + __static + "/app.html");
mainWindow.webContents.openDevTools()
mainWindow.webContents.once("did-finish-load", function () {
var http = require("http");
var server = http.createServer(function (req, res) {
console.log(req.url)
if (req.url == '/123') {
res.end(`ah, you send 123.`);
} else {
const remoteAddress = res.socket.remoteAddress;
const remotePort = res.socket.remotePort;
res.end(`Your IP address is ${remoteAddress} and your source port is ${remotePort}.`);
}
});
server.listen(PORT);
console.log("http://localhost:"+PORT);
});
});
发布于

2023-10-19

更新于

2023-10-25

许可协议

评论