react-scripts start 控制只允许127.0.0.1 访问
不会再显示 “On Your Network”。但如果你曾经修改过 host 设置(例如。这一行显示出来,则说明局域网访问被允许。),那就需要恢复或重新限制。能访问,就需要禁用这部分。,默认配置就已经是这样的;,不会监听局域网 IP。
·
react-scripts start 只允许127.0.0.1 访问
如果你想只允许本机访问、不允许局域网访问,默认配置就已经是这样的;
但如果你曾经修改过 host 设置(例如 HOST=0.0.0.0),那就需要恢复或重新限制。
✅ 一、确认当前行为
执行:
npm start
或
react-scripts start
默认输出类似:
Starting the development server...
Local: http://localhost:3000
On Your Network: http://192.168.1.100:3000
如果 “On Your Network” 这一行显示出来,则说明局域网访问被允许。
如果你只想让 127.0.0.1 能访问,就需要禁用这部分。
🚫 二、禁止局域网访问的做法
方法 1:通过环境变量限制 host
在启动命令前设置:
HOST=127.0.0.1 react-scripts start
或在 .env 文件中添加:
HOST=127.0.0.1
这样服务器只绑定 127.0.0.1,不会监听局域网 IP。
启动后输出应类似:
Local: http://localhost:3000
不会再显示 “On Your Network”。
方法 2:在 package.json 修改启动命令
找到你的 package.json:
"scripts": {
"start": "HOST=127.0.0.1 react-scripts start"
}
✅ Windows 用户要写成:
"start": "set HOST=127.0.0.1 && react-scripts start"
更多推荐
所有评论(0)