forked from yydfys/CatPawOpen
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdev.js
58 lines (49 loc) · 1.7 KB
/
dev.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {createServer} from 'http';
import os from 'os';
import {start} from './index.js';
import * as config from './index.config.js';
import axios from "axios";
globalThis.catServerFactory = (handle) => {
let port = 0;
const server = createServer((req, res) => {
handle(req, res);
});
// 自动 GET 请求逻辑
async function autoRequest(url) {
try {
const response = await axios.get(url); // 替换为目标接口
console.log(`Auto-request successful: ${response.data.message || ''}`);
} catch (error) {
console.log(`Auto-request failed: ${error.message}`);
}
}
server.on('listening', async () => {
port = server.address().port;
// Get local IP addresses
const networkInterfaces = os.networkInterfaces();
const addresses = [];
for (const interfaceName in networkInterfaces) {
for (const net of networkInterfaces[interfaceName]) {
if (!net.internal && net.family === 'IPv4') {
addresses.push(net.address);
}
}
}
const localConfigUrl = `http://localhost:${port}/config`;
console.log(`Server is running:`);
console.log(`- Local: ${localConfigUrl}`);
if (addresses.length > 0) {
console.log(`- Network: http://${addresses[0]}:${port}/config`);
}
console.log(`- Node.js version: ${process.version}`);
await autoRequest(localConfigUrl);
});
server.on('close', () => {
console.log('Server closed on port ' + port);
});
return server;
};
globalThis.catDartServerPort = () => {
return 0;
};
start(config.default);