如何通过nodejs快速搭建本地局域网环境?

希望通过node.js快速搭建一个本地局域网环境,来让同局域网的设备访问下载文件以及访问html文件。
有相关的npm包或技术推荐吗?

阅读 5.1k
4 个回答

node-static

npm i -g node-static
static --help
USAGE: /Users/svpg/.config/yarn/global/node_modules/.bin/static [-p <port>] [<directory>]

simple, rfc 2616 compliant file streaming module for node

Options:
  --port, -p          TCP port at which the files will be served                                                          [default: 8080]
  --host-address, -a  the local network interface at which to listen                                                      [default: "127.0.0.1"]
  --cache, -c         "Cache-Control" header setting, defaults to 3600                                                  
  --version, -v       node-static version                                                                               
  --headers, -H       additional headers (in JSON format)                                                               
  --header-file, -f   JSON file of additional headers                                                                   
  --gzip, -z          enable compression (tries to serve file of same name plus '.gz')                                  
  --spa               serve the content as a single page app by redirecting all non-file requests to the index html file
  --indexFile, -i     specify a custom index file when serving up directories                                             [default: "index.html"]
  --help, -h          display this help message                                                                         

使用:

# 监听所有网卡的9999端口
static -p 9999 -a 0.0.0.0
npm i anywhere -g
//打开命令行,在你的项目或者桌面路径下
anywhere 8080
//同一个局域网都可以访问你电脑的ip:8080了

anywhere了解一下

npm i http-server -g
// 想要host的目录下
http-server 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题