From d1643f054ed27ef56741a5431cbd6e102f0f94b7 Mon Sep 17 00:00:00 2001 From: Kubbo <390378816@qq.com> Date: Wed, 9 Apr 2025 11:01:02 +0800 Subject: [PATCH] =?UTF-8?q?feat(env):=20=E6=B7=BB=E5=8A=A0=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE=E5=B9=B6=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 .env 文件,定义服务器端口和 IP 地址 - 创建 .gitignore 文件,忽略 node_modules 目录 - 修改 start.js,从环境变量中读取配置 - 初始化 package-lock.json 和 package.json - 添加 dotenv 依赖用于加载环境变量 --- .env | 5 +++++ .gitignore | 1 + GateServer/start.js | 7 ++++++- package-lock.json | 27 +++++++++++++++++++++++++++ package.json | 5 ++++- 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 .env create mode 100644 .gitignore create mode 100644 package-lock.json diff --git a/.env b/.env new file mode 100644 index 0000000..eb737b6 --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +# 定义服务器监听的端口号 +GATE_PORT=9001 + +# 定义服务器监听的IP地址 +GATE_HOST=127.0.0.1 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/GateServer/start.js b/GateServer/start.js index 7ea1a21..b07651a 100644 --- a/GateServer/start.js +++ b/GateServer/start.js @@ -1,6 +1,11 @@ +require('dotenv').config(); // 引入dotenv库并加载.env文件中的环境变量 const GateServer = require('./index'); -const server = new GateServer(8080, '127.0.0.1'); +// 从环境变量中读取端口和IP地址 +const port = parseInt(process.env.GATE_PORT, 10); +const host = process.env.GATE_HOST; + +const server = new GateServer(port, host); server.on('connection', (socket) => { console.log('A new client has connected'); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..8849dd5 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "node-mir-server", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-mir-server", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "dotenv": "^16.4.7" + } + }, + "node_modules/dotenv": { + "version": "16.4.7", + "resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.4.7.tgz", + "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + } + } +} diff --git a/package.json b/package.json index 17f411c..6b4857f 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,8 @@ "server" ], "author": "kubbo", - "license": "ISC" + "license": "ISC", + "dependencies": { + "dotenv": "^16.4.7" + } }