| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- // const { SocketTunnel } = require('@d8d-socket-tunnel/client');
- const http = require("http");
- const debug = require('debug')('tunnel:local');
- const server = http.createServer((req, res) => {
- console.log(`收到请求: ${req.method} ${req.url}`); // 添加请求日志
- res.statusCode = 200;
- res.setHeader("Content-Type", "text/plain");
- res.end("Hello, World!");
- });
- const port = process.env.LOCAL_PORT;
- // // 创建隧道实例,使用环境变量或默认值
- // const tunnel = new SocketTunnel({
- // serverUrl: process.env.TUNNEL_SERVER, // 如果未设置,将使用默认值
- // localPort: port // 如果未设置,将使用默认值
- // });
- // // 连接到隧道服务器
- // tunnel.connect();
- // 启动 HTTP 服务器
- server.listen(port, async () => {
- debug(`本地服务器启动在端口 ${port}`);
-
- // 优雅退出
- process.on("SIGINT", () => {
- debug('收到退出信号');
- // if (tunnel) {
- // tunnel.close();
- // debug('隧道已主动关闭');
- // }
- server.close(() => {
- debug('服务器已关闭');
- process.exit(0);
- });
- });
- });
|