Browse Source

✨ feat(server): 重构服务器启动逻辑并调整默认端口

- 将默认端口从5173更改为8080
- 重构服务器启动代码,提取parentServer变量
- 配置Vite开发服务器的HMR设置,使用8081端口和WebSocket代理

📝 docs(app): 更新页面标题文本

- 修改App.tsx中的标题文本从"Vite + React"改为"Vite + React1248898"
yourname 7 months ago
parent
commit
63af745803
3 changed files with 30 additions and 13 deletions
  1. 28 11
      server.js
  2. 1 1
      src/App.tsx
  3. 1 1
      src/entry-server.tsx

+ 28 - 11
server.js

@@ -6,19 +6,30 @@ import { pipeline } from 'node:stream/promises';
 import { serve } from '@hono/node-server';
 import { serve } from '@hono/node-server';
 import { Hono } from 'hono';
 import { Hono } from 'hono';
 import { createServer as createNodeServer } from 'node:http';
 import { createServer as createNodeServer } from 'node:http';
+import process from 'node:process';
 
 
 // 创建 Hono 应用
 // 创建 Hono 应用
 const app = new Hono();
 const app = new Hono();
 
 
 // Constants
 // Constants
 const isProduction = process.env.NODE_ENV === 'production';
 const isProduction = process.env.NODE_ENV === 'production';
-const port = process.env.PORT || 5173;
+const port = process.env.PORT || 8080;
 const base = process.env.BASE || '/';
 const base = process.env.BASE || '/';
 const ABORT_DELAY = 10000;
 const ABORT_DELAY = 10000;
 
 
 // 解析基础路径为 URL 对象,方便后续处理
 // 解析基础路径为 URL 对象,方便后续处理
 const baseUrl = new URL(base, `http://localhost:${port}`);
 const baseUrl = new URL(base, `http://localhost:${port}`);
 
 
+
+// 使用 Hono 的 serve 启动服务器
+const parentServer = serve({
+  fetch: app.fetch,
+  createServer: createNodeServer,
+  port: port,
+}, () => {
+  console.log(`Server started at http://localhost:${port}`);
+});
+
 // Cached production assets
 // Cached production assets
 let templateHtml = '';
 let templateHtml = '';
 if (isProduction) {
 if (isProduction) {
@@ -42,7 +53,22 @@ let vite;
 if (!isProduction) {
 if (!isProduction) {
   const { createServer } = await import('vite');
   const { createServer } = await import('vite');
   vite = await createServer({
   vite = await createServer({
-    server: { middlewareMode: true },
+    server: { middlewareMode: {
+        server: parentServer
+      },
+      hmr: {  
+        port: 8081,
+        clientPort: 443, // 或其他可用端口  
+        path: 'vite-hmr'
+      },
+      proxy: {
+        '/vite-hmr': {
+          target: 'ws://localhost:8081',
+          // Proxying WebSocket
+          ws: true,
+        },
+      },
+    },
     appType: 'custom',
     appType: 'custom',
     base,
     base,
   });
   });
@@ -181,12 +207,3 @@ app.use(async (c) => {
     return c.text(e.stack, 500);
     return c.text(e.stack, 500);
   }
   }
 });
 });
-
-// 使用 Hono 的 serve 启动服务器
-serve({
-  fetch: app.fetch,
-  createServer: createNodeServer,
-  port: port,
-}, () => {
-  console.log(`Server started at http://localhost:${port}`);
-});

+ 1 - 1
src/App.tsx

@@ -16,7 +16,7 @@ function App() {
           <img src={reactLogo} className="logo react" alt="React logo" />
           <img src={reactLogo} className="logo react" alt="React logo" />
         </a>
         </a>
       </div>
       </div>
-      <h1>Vite + React</h1>
+      <h1>Vite + React1248898</h1>
 
 
       <Suspense fallback={<p>Loading card component...</p>}>
       <Suspense fallback={<p>Loading card component...</p>}>
         <Card />
         <Card />

+ 1 - 1
src/entry-server.tsx

@@ -12,4 +12,4 @@ export function render(_url: string, options?: RenderToPipeableStreamOptions) {
     </StrictMode>,
     </StrictMode>,
     options,
     options,
   )
   )
-}
+}