server2.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // const { SocketTunnel } = require('@d8d-socket-tunnel/client');
  2. const http = require("http");
  3. const debug = require('debug')('tunnel:local');
  4. const server = http.createServer((req, res) => {
  5. console.log(`收到请求: ${req.method} ${req.url}`); // 添加请求日志
  6. res.statusCode = 200;
  7. res.setHeader("Content-Type", "text/plain");
  8. res.end("Hello, World!");
  9. });
  10. const port = process.env.LOCAL_PORT;
  11. // // 创建隧道实例,使用环境变量或默认值
  12. // const tunnel = new SocketTunnel({
  13. // serverUrl: process.env.TUNNEL_SERVER, // 如果未设置,将使用默认值
  14. // localPort: port // 如果未设置,将使用默认值
  15. // });
  16. // // 连接到隧道服务器
  17. // tunnel.connect();
  18. // 启动 HTTP 服务器
  19. server.listen(port, async () => {
  20. debug(`本地服务器启动在端口 ${port}`);
  21. // 优雅退出
  22. process.on("SIGINT", () => {
  23. debug('收到退出信号');
  24. // if (tunnel) {
  25. // tunnel.close();
  26. // debug('隧道已主动关闭');
  27. // }
  28. server.close(() => {
  29. debug('服务器已关闭');
  30. process.exit(0);
  31. });
  32. });
  33. });