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


当前回答

以下内容对我有用:

创建包含以下内容的文件app.js:

// app.js

var fs = require('fs'),
    http = require('http');

http.createServer(function (req, res) {
  fs.readFile(__dirname + req.url, function (err,data) {
    if (err) {
      res.writeHead(404);
      res.end(JSON.stringify(err));
      return;
    }
    res.writeHead(200);
    res.end(data);
  });
}).listen(8080);

创建包含以下内容的文件index.html:

Hi

启动命令行:

cmd

在cmd中运行以下命令:

node app.js

转到下面的URL,以chrome显示:

http://localhost:8080/index.html

这就是全部。希望这会有所帮助。

资料来源:https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

其他回答

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

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

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

使用connect的简单静态服务器

var connect = require('connect'),
  directory = __dirname,
  port = 3000;

connect()
  .use(connect.logger('dev'))
  .use(connect.static(directory))
  .listen(port);

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

另请参见将node.js用作简单的web服务器

以下内容对我有用:

创建包含以下内容的文件app.js:

// app.js

var fs = require('fs'),
    http = require('http');

http.createServer(function (req, res) {
  fs.readFile(__dirname + req.url, function (err,data) {
    if (err) {
      res.writeHead(404);
      res.end(JSON.stringify(err));
      return;
    }
    res.writeHead(200);
    res.end(data);
  });
}).listen(8080);

创建包含以下内容的文件index.html:

Hi

启动命令行:

cmd

在cmd中运行以下命令:

node app.js

转到下面的URL,以chrome显示:

http://localhost:8080/index.html

这就是全部。希望这会有所帮助。

资料来源:https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/

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

npx serve

or

npx http-server