Node.js快速文件服务器(通过HTTP的静态文件)

Node.js

神无蛋蛋

2020-03-16

是否有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中的基本静态文件服务器

第1693篇《Node.js快速文件服务器(通过HTTP的静态文件)》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

13个回答
西里阿飞 2020.03.16

I use Houston at work and for personal projects, it works well for me.

https://github.com/alejandro/Houston

老丝猿阿飞 2020.03.16

If you are intrested in ultra-light http server without any prerequisites you should have a look at: mongoose

三三呀 2020.03.16

For dev work you can use (express 4) https://github.com/appsmatics/simple-httpserver.git

小宇宙A 2020.03.16

From npm@5.2.0, npm started installing a new binary alongside the usual npm called npx. So now, one liners to create static http server from current directory:

npx serve

or

npx http-server
飞云逆天 2020.03.16

首先通过npm install node-static -g -g 安装节点静态服务器是在系统上全局安装它,然后导航到文件所在的目录,并使用static 它在端口8080上侦听启动服务器,并通过naviaget浏览器并键入localhost:8080 / yourhtmlfilename 。

神无JinJin 2020.03.16

它尚未安装在NPM上,但是我在Express上构建了一个简单的静态服务器,该服务器还允许您接受表单提交并通过事务性电子邮件服务通过电子邮件发送它们(目前为Sendgrid,Mandrill即将推出)。

https://github.com/jdr0dn3y/nodejs-StatServe

凯樱 2020.03.16

这是另一个简单的Web服务器。

https://www.npmjs.com/package/hostr

安装

npm install -g hostr

更换工作总监

cd myprojectfolder/

然后开始

hostr
Tony西门古一 2020.03.16

是我的一个文件/轻量级的node.js静态文件Web服务器宠物项目,它没有依赖性,我相信这是一个快速而丰富的工具,其使用就像在Linux / Unix / macOS终端上发出此命令一样简单(如果安装了node.js(或在Debian / Ubuntu上),则为Android termuxnodejs-legacy

curl pad.js.org | node 

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

它支持我认为可能有用的不同事物,

  • 分层目录索引的创建/服务
    • 具有不同标准的分类能力
    • 在Chrome,Firefox和其他浏览器上通过[多文件]拖放和仅文件/文本复制粘贴以及系统剪贴板屏幕截图粘贴从浏览器上载可能会有一些限制(可以通过命令行关闭它提供的选项)
    • 文件夹/创建笔记/上传按钮
  • Serving correct MIMEs for well known file types (with possibility for disabling that)
  • Possibility of installation as a npm package and local tool or, one-linear installation as a permanent service with Docker
  • HTTP 206 file serving (multipart file transfer) for faster transfers
  • Uploads from terminal and browser console (in fact it was originally intended to be a file-system proxy for JS console of browsers on other pages/domains)
  • CORS download/uploads (which also can be turned off)
  • Easy HTTPS integration
  • Lightweight command line options for achieving better secure serving with it:
    • With my patch on node.js 8, you can have access to the options without first installation: curl pad.js.org | node - -h
    • Or first install it as a system-global npm package by [sudo] npm install -g pad.js and then use its installed version to have access to its options: 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上,欢迎您提供反馈,功能请求和⭐!

西门泡芙Jim 2020.03.16

如果使用Express框架,则可以使用此功能。

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

mkdir yourapp
cd yourapp
npm install express
node_modules/express/bin/express
JinJin泡芙 2020.03.16

还有另一个非常不错的静态Web服务器:浏览器同步。

可以使用节点包管理器下载它:

npm install -g browser-sync

安装后,在cmd提示符下导航到项目文件夹,然后运行以下命令:

browser-sync start --server --port 3001 --files="./*"

它将开始处理浏览器当前文件夹中的所有文件。

可以从BrowserSync中找到更多信息

谢谢。

猪猪GO 2020.03.16

One-line™证明而非承诺

在此处输入图片说明

第一个是http-serverhs- 链接

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

第二个是serve通过ZEIT.co- 链接

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

Following are available options, if this is what helps you decide.

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在日志消息中使用UTC时间格式。

  -P --proxy后备代理(如果无法解决请求)。例如:http://someurl.com

  -S --ssl启用https。
  -C --cert ssl证书文件的路径(默认值:cert.pem)。
  -K --key ssl密钥文件的路径(默认值:key.pem)。

  -r --robots响应/robots.txt [用户代理:* \ n禁止:/]
  -h --help打印此列表并退出。
C:\ Users \ Qwerty>服务-帮助

  用法:serve.js [选项] [命令]

  命令:

    帮助显示帮助

  选项:

    -a,--auth服务于基本身份验证
    -c,--cache在浏览器中缓存文件的时间(以毫秒为单位)
    -n,--clipless不将地址复制到剪贴板(默认情况下禁用)
    -C,-cors设置* CORS标头,以允许来自任何来源的请求(默认情况下禁用)
    -h,--help输出用法信息
    -i,--ignore要忽略的文件和目录
    -o,--open在浏览器中打开本地地址(默认禁用)
    -p,--port监听的端口(默认为5000)
    -S,--silent不要将任何内容记录到控制台
    -s,--single服务单页应用程序(将-c设置为1天)
    -t,--treeless不显示静态树(默认情况下禁用)
    -u,--unzipped禁用GZIP压缩
    -v,--version输出版本号

如果您需要注意更改,请参见hostr,相信曾亨利的回答

Jim猪猪 2020.03.16

我知道它不是Node,但是我使用了Python的SimpleHTTPServer:

python -m SimpleHTTPServer [port]

它运行良好,并带有Python。

Eva神无 2020.03.16

一个很好的“即用型工具”选项可以是http-server:

npm install http-server -g

要使用它:

cd D:\Folder
http-server

或者像这样:

http-server D:\Folder

签出:https : //github.com/nodeapps/http-server

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android