feat(env): 添加环境变量配置并初始化项目
- 新增 .env 文件,定义服务器端口和 IP 地址 - 创建 .gitignore 文件,忽略 node_modules 目录 - 修改 start.js,从环境变量中读取配置 - 初始化 package-lock.json 和 package.json - 添加 dotenv 依赖用于加载环境变量
This commit is contained in:
5
.env
Normal file
5
.env
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# 定义服务器监听的端口号
|
||||||
|
GATE_PORT=9001
|
||||||
|
|
||||||
|
# 定义服务器监听的IP地址
|
||||||
|
GATE_HOST=127.0.0.1
|
||||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
node_modules/
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
|
require('dotenv').config(); // 引入dotenv库并加载.env文件中的环境变量
|
||||||
const GateServer = require('./index');
|
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) => {
|
server.on('connection', (socket) => {
|
||||||
console.log('A new client has connected');
|
console.log('A new client has connected');
|
||||||
|
|||||||
27
package-lock.json
generated
Normal file
27
package-lock.json
generated
Normal file
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,5 +10,8 @@
|
|||||||
"server"
|
"server"
|
||||||
],
|
],
|
||||||
"author": "kubbo",
|
"author": "kubbo",
|
||||||
"license": "ISC"
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^16.4.7"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user