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


当前回答

NPM上还没有,但我在Express上构建了一个简单的静态服务器,它还允许您接受表单提交,并通过事务电子邮件服务(现在是Sendgrid,Mandrill即将到来)发送电子邮件。

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

其他回答

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

python -m SimpleHTTPServer [port]

它运行良好,并与Python一起提供。

为了使用节点服务静态资源来健康地提高性能,我建议使用Buffet。它的工作方式类似于web应用程序加速器,也称为缓存HTTP反向代理,但它只是将所选目录加载到内存中。

Buffet采用了一种完全缓冲的方法——当您的应用程序启动时,所有文件都会完全加载到内存中,因此您永远不会感觉到文件系统被烧毁。实际上,这是非常有效的。如此之多,以至于将Varnish放在应用程序前面甚至可能会使其变慢! 

我们在codePile网站上使用它,发现在1k并发用户连接负载下下载25个资源的页面上,请求数增加了约700个/秒,达到超过4k个/秒。

例子:

var server = require('http').createServer();

var buffet = require('buffet')(root: './file'); 

 

server.on('request', function (req, res) {

  buffet(req, res, function () {

    buffet.notFound(req, res);

  });

});

 

server.listen(3000, function () {

  console.log('test server running on port 3000');

});

连接可能是你想要的。

易于安装:

npm install connect

那么最基本的静态文件服务器可以写为:

var connect = require('connect'),
    directory = '/path/to/Folder';

connect()
    .use(connect.static(directory))
    .listen(80);

console.log('Listening on port 80.');

#仅演示/原型服务器

如果您只需要这些,请尝试以下操作:

const fs = require('fs'),
      http = require('http'),
      arg = process.argv.slice(2),
      rootdir = arg[0] || process.cwd(),
      port = process.env.PORT || 9000,
      hostname = process.env.HOST || '127.0.0.1';
//tested on node=v10.19.0
http.createServer(function (req, res) {

  try {
    // change 'path///to/////dir' -> 'path/to/dir'
    req_url = decodeURIComponent(req.url).replace(/\/+/g, '/');

    stats = fs.statSync(rootdir + req_url);

    if (stats.isFile()) {
      buffer = fs.createReadStream(rootdir + req_url);
      buffer.on('open', () => buffer.pipe(res));
      return;
    }

    if (stats.isDirectory()) {
      //Get list of files and folder in requested directory
      lsof = fs.readdirSync(rootdir + req_url, {encoding:'utf8', withFileTypes:false});
      // make an html page with the list of files and send to browser
      res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
      res.end(html_page(`http://${hostname}:${port}`, req_url, lsof));
      return;
    }

  } catch (err) {
      res.writeHead(404);
      res.end(err);
      return;
  }
}).listen(port, hostname, () => console.log(`Server running at http://${hostname}:${port}`));


function html_page(host, req_url, lsof) {//this is a Function declarations can be called before it is defined
  // Add link to root directory and parent directory if not already in root directory
  list = req_url == '/' ? [] : [`<a href="${host}">/</a>`,
  `<a href="${host}${encodeURI(req_url.slice(0,req_url.lastIndexOf('/')))}">..</a>`];

  templete = (host, req_url, file) => {// the above is a Function expressions cannot be called before it is defined
    return `<a href="${host}${encodeURI(req_url)}${req_url.slice(-1) == '/' ? '' : '/'}${encodeURI(file)}">${file}</a>`; }

  // Add all the links to the files and folder in requested directory
  lsof.forEach(file => {
    list.push(templete(host, req_url, file));
  });

  return `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html" charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Directory of ${req_url}</title>
</head>
<body>
<h2>Directory of ${req_url}</h2>
${list.join('<br/>\n')}
</body>
</html>`
}

Node.js上的小型命令行web服务器:miptleha http

完整源代码(80行)