Parcourir la source

新增局域网服务访问地址

zhusiqing il y a 3 ans
Parent
commit
0a0378be4b
2 fichiers modifiés avec 16 ajouts et 0 suppressions
  1. 3 0
      src/app.ts
  2. 13 0
      src/utils/getLocalIp.ts

+ 3 - 0
src/app.ts

@@ -25,6 +25,7 @@ import sessionMiddleware from './middlewares/session';
 import localDbMiddleware from './middlewares/localDb';
 
 import { loggerInstance } from '@utils/logger';
+import { getLocalIp } from '@utils/getLocalIp';
 
 const app = new Koa<DefaultState, Context>();
 
@@ -75,5 +76,7 @@ app.use(router.routes()).use(router.allowedMethods());
 
 const port = config.port;
 app.listen(port, () => {
+  const ip = getLocalIp()
   consola.success(`server is started at: http://localhost:${port}`)
+  consola.success(`server is started at: http://${ip}:${port}`)
 });

+ 13 - 0
src/utils/getLocalIp.ts

@@ -0,0 +1,13 @@
+import { networkInterfaces } from 'os';
+
+export function getLocalIp() {
+  const ips = networkInterfaces()
+  let ip
+  Object.values(ips).forEach(el => {
+    const matchIp = el?.find(element => element.family === 'IPv4')
+    if (matchIp && matchIp.address !== '127.0.0.1') {
+      ip = matchIp.address
+    }
+  })
+  return ip
+}