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


当前回答

单线™ 证明而不是承诺

第一个是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

从…起npm@5.2.0,npm开始在通常的npm旁边安装一个新的二进制文件npx。现在,使用一行代码从当前目录创建静态http服务器:

npx serve

or

npx http-server

在NPM注册表中搜索https://npmjs.org/search?q=server,我找到了静态服务器https://github.com/maelstrom/static-server

曾经需要向同事发送文件,但不必麻烦发送电子邮件100MB的野兽?想要运行一个简单的JavaScript示例应用程序,但在文件中运行时遇到问题:///协议希望在没有设置Samba、FTP或其他需要您编辑的内容配置文件?然后这个文件服务器会让你的生活简单一点。要安装简单的静态素材服务器,请使用npm:npm安装-g静态服务器然后,要提供文件或目录,只需运行$serve路径/to/stuff在端口8001上提供路径/到/填充

这甚至可以列出文件夹内容。

很遗憾,它无法提供文件:)

const http=要求('http');常量fs=要求('fs');const url=require('url');常量路径=require('path');让mimeTypes={“.html”:“text/html”,“.css”:“text/css”,“.js”:“text/javascript”,“.jpg”:“image/jpeg”,'.png':'image/png','.ico':'image/x-icon',“.svg”:“image/svg+xml”,“.eot”:“appaction/vnd.ms fontobject”,“.ttf”:“应用程序/font sfnt”};http.createServer(函数(请求、响应){let pathName=url.parse(request.url).path;如果(路径名=='/'){pathName='/index.html';}pathName=pathName.substring(1,pathName.length);let extName=路径.extName(路径名);let staticFiles=`${__dirname}/template/${pathName}`;如果(extName==“.jpg”| | extName=“.png”| | ext Name=“.ico”| | extName=“.eot”| | extName=“.ttf”| | textName=“.svg”){let file=fr.readFileSync(静态文件);res.writeHead(200,{'Content-Type':mimeTypes[extname]});res.write(文件,'binary');res.end();}其他{fs.readFile(staticFiles,'utf8',函数(错误,数据){if(!err){res.writeHead(200,{'Content-Type':mimeTypes[extname]});res.end(数据);}其他{res.writeHead(404,{'Content-Type':'text/html;charset=utf8'});res.write(“<strong>${staticFiles}</strong>找不到文件。”);}res.end();});}}).听(8081);

首先通过npm安装node static服务器,将其全局安装到系统上,然后导航到文件所在的目录,在端口8080上启动静态服务器,导航到浏览器并键入localhost:8080/yourhtmlfilename。