本文翻译自:Node.js quick file server (static files over HTTP)
Is there Node.js ready-to-use tool (installed with npm
), that would help me expose folder content as file server over HTTP. 是否有Node.js即用型工具(随npm
安装),它将帮助我通过HTTP将文件夹内容作为文件服务器公开。
Example, if I have 例如,如果我有
D:\Folder\file.zip
D:\Folder\file2.html
D:\Folder\folder\file-in-folder.jpg
Then starting in D:\\Folder\\
node node-file-server.js
I could access file via 然后从D:\\Folder\\
node node-file-server.js
我可以通过
http://hostname/file.zip
http://hostname/file2.html
http://hostname/folder/file-in-folder.jpg
Why is my node static file server dropping requests? 为什么我的节点静态文件服务器删除请求? reference some mystical 引用一些神秘的东西
standard node.js static file server 标准的node.js静态文件服务器
If there's no such tool, what framework should I use? 如果没有这样的工具,我应该使用什么框架?
Related: Basic static file server in NodeJS 相关: NodeJS中的基本静态文件服务器
#1楼
参考:https://stackoom.com/question/16XAE/Node-js快速文件服务器-通过HTTP的静态文件
#2楼
If you use the Express framework , this functionality comes ready to go. 如果使用Express框架 ,则可以使用此功能。
To setup a simple file serving app just do this: 要设置一个简单的文件服务应用,只需执行以下操作:
mkdir yourapp
cd yourapp
npm install express
node_modules/express/bin/express
#3楼
connect could be what you're looking for. 连接可能是您想要的。
Installed easily with: 易于安装:
npm install connect
Then the most basic static file server could be written as: 然后,最基本的静态文件服务器可以写成:
var connect = require('connect'),
directory = '/path/to/Folder';
connect()
.use(connect.static(directory))
.listen(80);
console.log('Listening on port 80.');
#4楼
Searching in NPM registry https://npmjs.org/search?q=server , I have found static-server https://github.com/maelstrom/static-server 在NPM注册表https://npmjs.org/search?q=server中搜索,我找到了静态服务器https://github.com/maelstrom/static-server
Ever needed to send a colleague a file, but can't be bothered emailing the 100MB beast? 是否曾经需要向同事发送文件,但是是否可以通过电子邮件发送100MB的野兽? Wanted to run a simple example JavaScript application, but had problems with running it through the file:/// protocol? 是否想运行一个简单的示例JavaScript应用程序,但是在通过file:///协议运行它时遇到问题? Wanted to share your media directory at a LAN without setting up Samba, or FTP, or anything else requiring you to edit configuration files? 是否想在不设置Samba或FTP或其他任何需要编辑配置文件的情况下在LAN上共享媒体目录? Then this file server will make your life that little bit easier. 然后,此文件服务器将使您的生活变得更加轻松。
To install the simple static stuff server, use npm: 要安装简单的静态填充服务器,请使用npm:
npm install -g static-server
Then to serve a file or a directory, simply run 然后要提供文件或目录,只需运行
$ serve path/to/stuff Serving path/to/stuff on port 8001
That could even list folder content. 甚至可以列出文件夹内容。
Unfortunately, it couldn't serve files :) 不幸的是, 它无法提供文件 :)
#5楼
I use Houston at work and for personal projects, it works well for me. 我在工作和个人项目中使用休斯顿,对我来说效果很好。
https://github.com/alejandro/Houston https://github.com/alejandro/休斯顿
#6楼
I know it's not Node, but I've used Python's SimpleHTTPServer: 我知道它不是Node,但是我使用了Python的SimpleHTTPServer:
python -m SimpleHTTPServer [port]
It works well and comes with Python. 它运行良好,并带有Python。