程序启动时,端口占用问题

前提条件

  • 本机 IP:
    1. localhost
    2. 127.0.0.1
    3. 192.168.1.5
    4. 172.25.25.9
  • 程序端口:5173

说明

  1. 程序启动没有指定监听 IP 的情况下,默认监听本机所有的 IP,即:可以使用 127.0.0.1:端口本机任意一个内网IP:端口 访问程序,比如:localhost:5173127.0.0.1:5173192.168.1.5:5173172.25.25.9:5173
  2. 程序启动指定监听 IP 的情况下,只能通过 监听的IP:端口 访问程序,比如:程序启动时,仅监听了127.0.0.1,只能通过 127.0.0.1:5173 访问程序,使用 192.168.1.5:5173172.25.25.9:8080 无法访问程序

示例

  1. 创建项目命令
    npm add -D vitepress@next
    npx vitepress init
    
  2. 启动项目命令
    npm run docs:dev
    
  3. 由于 vitepress 默认仅仅监听 localhost,所以只能使用 http://localhost:5173 访问 vitepress 程序,而不能使用 http://127.0.0.1:5173 访问程序
  4. package.json 中的 scripts 添加 "docs:dev:host": "vitepress dev --host",然后使用 npm run docs:dev:host 启动程序,就可以使用本机任意 IP 访问 vitepress 了,比如:
    1. http://localhost:5173
    2. http://127.0.0.1:5173
    3. http://192.168.1.5:5173
    4. http://172.25.25.9:5173