使用node.js的http-server開啟一個本地服務器


用html寫了一個網頁,想要在手機上查看適配效果,但是苦於手機上沒有直接查看HTML的。想到手機和電腦都在一個局域網內,能不能搭建一個局域網內的網頁服務器呢?

1、下載 http-server

顯然,需要先下載 node.js(不寫了)

安裝 http-server:

npm install http-server -g

2、開啟 http-server 服務

終端進入目標文件夾,然后在終端輸入:

C:\Users\20928\Documents\VS_Code\Html>http-server -c-1  (⚠️只輸入http-server的話,更新了代碼后,頁面不會同步更新)
Starting up http-server, serving ./
Available on:
  http://172.17.151.1:8080
  http://192.168.159.1:8080
  http://192.168.176.1:8080
  http://127.0.0.1:8080
  http://192.168.2.200:8080

然后,訪問上面的網址,發現打不開......

解決方法:安裝 anywhere

anywhere安裝命令:npm install -g anywhere

再運行命令:anywhere -p 8080(-p 后跟端口號)

C:\Users\20928\Documents\VS_Code\Html>anywhere -p 8080
Running at http://172.17.151.1:8080/
Also running at https://172.17.151.1:8081/

//此時,實際能訪問的網址不只這兩個

其實,不安裝anywhere也能訪問:只需在網址后面加上文件名

例如,http://172.17.151.1:8080/hello.html

3、效果

此時,在手機端也能訪問該頁面。

但是好像只能用主機在局域網內的那個IP訪問,通常為192.168.X.X

 

 

參考鏈接:

1. https://www.cnblogs.com/wukaiBK/p/11386614.html

2. https://blog.csdn.net/xigua3188/article/details/99542884


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM