是否有Node.js即用工具(与npm一起安装),可以帮助我通过HTTP将文件夹内容作为文件服务器公开。

例如,如果我有

D:\Folder\file.zip
D:\Folder\file2.html
D:\Folder\folder\file-in-folder.jpg

然后从D:\Folder\node node-file-server.js开始我可以通过

http://hostname/file.zip
http://hostname/file2.html
http://hostname/folder/file-in-folder.jpg

为什么我的节点静态文件服务器丢弃请求?参考一些神秘的

标准node.js静态文件服务器

如果没有这样的工具,我应该使用什么框架?

相关:NodeJS中的基本静态文件服务器


当前回答

对于开发工作,您可以使用(express 4)https://github.com/appsmatics/simple-httpserver.git

其他回答

这是我的一个文件/轻量级node.js静态文件web服务器宠物项目,我相信它是一个快速而丰富的工具,当安装node.js(或Debian/Uubuntu上的nodejs遗留版本)时,它的使用就像在Linux/Unix/macOS终端(或Android上的termux)上发出这个命令一样简单:

curl pad.js.org | node 

(文档中有针对Windows用户的不同命令)

它支持不同的东西,我认为这些东西是有用的,

分层目录索引创建/服务具有不同标准的排序功能在Chrome、Firefox和其他浏览器上,通过[多文件]拖放和仅文件/文本复制粘贴以及系统剪贴板屏幕截图粘贴从浏览器上传可能存在一些限制(可以通过其提供的命令行选项关闭)文件夹/便笺创建/上传按钮为已知文件类型提供正确的MIME(可能禁用)可以作为npm包和本地工具进行安装,也可以作为Docker的永久服务进行线性安装HTTP 206文件服务(多部分文件传输),用于更快的传输从终端和浏览器控制台上传(事实上,它最初打算作为其他页面/域浏览器JS控制台的文件系统代理)CORS下载/上传(也可以关闭)轻松的HTTPS集成轻量级命令行选项,可实现更好的安全服务:使用node.js 8上的补丁,您无需先安装即可访问选项:curl pad.js.org | node--h或者先通过[sudo]npm install-g pad.js将其安装为系统全局npm包,然后使用其安装版本访问其选项:pad-h或者使用提供的Docker镜像,默认情况下使用相对安全的选项。[sudo]docker run--restart=always-v/files:/files--name pad.js-d-p 9090:9090 quay.io/ebraminio/pad.js

上述功能主要记录在工具主页上http://pad.js.org我使用了一些很好的技巧,这也是工具源本身的来源!

工具来源于GitHub,欢迎您的反馈、功能请求和⭐s

你可以使用NPM服务包来实现这一点,如果你不需要NodeJS的东西,它是一个快速易用的工具:

1-在电脑上安装软件包:

npm install -g serve

2-使用Serve<path>服务静态文件夹:

d:> serve d:\StaticSite

它将显示静态文件夹的服务端口,只需导航到主机,如下所示:

http://localhost:3000

单线™ 证明而不是承诺

第一个是http服务器,hs-link

npm i -g http-server   // install
hs C:\repos            // run with one line?? FTW!!

第二个由ZEIT.co提供-链接

npm i -g serve         // install
serve C:\repos         // run with one line?? FTW!!

以下是可用选项,如果这有助于您做出决定。

C:\Users\Qwerty>http-server --help
usage: http-server [path] [options]

options:
  -p           Port to use [8080]
  -a           Address to use [0.0.0.0]
  -d           Show directory listings [true]
  -i           Display autoIndex [true]
  -g --gzip    Serve gzip files when possible [false]
  -e --ext     Default file extension if none supplied [none]
  -s --silent  Suppress log messages from output
  --cors[=headers]   Enable CORS via the "Access-Control-Allow-Origin" header
                     Optionally provide CORS headers list separated by commas
  -o [path]    Open browser window after starting the server
  -c           Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.
               To disable caching, use -c-1.
  -U --utc     Use UTC time format in log messages.

  -P --proxy   Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com

  -S --ssl     Enable https.
  -C --cert    Path to ssl cert file (default: cert.pem).
  -K --key     Path to ssl key file (default: key.pem).

  -r --robots  Respond to /robots.txt [User-agent: *\nDisallow: /]
  -h --help    Print this list and exit.
C:\Users\Qwerty>serve --help

  Usage: serve.js [options] [command]

  Commands:

    help  Display help

  Options:

    -a, --auth      Serve behind basic auth
    -c, --cache     Time in milliseconds for caching files in the browser
    -n, --clipless  Don't copy address to clipboard (disabled by default)
    -C, --cors      Setup * CORS headers to allow requests from any origin (disabled by default)
    -h, --help      Output usage information
    -i, --ignore    Files and directories to ignore
    -o, --open      Open local address in browser (disabled by default)
    -p, --port   Port to listen on (defaults to 5000)
    -S, --silent    Don't log anything to the console
    -s, --single    Serve single page applications (sets `-c` to 1 day)
    -t, --treeless  Don't display statics tree (disabled by default)
    -u, --unzipped  Disable GZIP compression
    -v, --version   Output the version number

如果你需要留意变化,请看主持人,感谢曾亨利的回答

如果您使用Express框架,此功能随时可用。

要设置简单的文件服务应用程序,只需执行以下操作:

mkdir yourapp
cd yourapp
npm install express
node_modules/express/bin/express

你可以试着为我服务

使用它非常简单:

ServeMe = require('serve-me')();
ServeMe.start(3000);

仅此而已。

PD:默认情况下提供的文件夹为“public”。