使用快递服务静态文件最简单的方法是什么?
我使用一个相当丑陋的方法:
var app = require('express')(), server = require('http').createServer(app), fs = require('fs'); server.listen(80); path = "/Users/my/path/"; var served_files = {}; ["myfile1.html","myfile2.html","myfile3.html"].forEach(function(file){ served_files["/"+file] = fs.readFileSync(path+file,"utf8"); }); app.use(function(req,res){ if (served_files[req.path]) res.send(files[req.path]); });
什么是正确的方法来做到这一点?
Express有一个内置的中间件。 它是连接的一部分,expression是build立在连接上的。 中间件本身使用send 。
// just add the middleware to your app stack via `use` app.use(express.static(yourpath));
回答你的评论,不,没有办法手动select文件。 尽pipe默认情况下,中间件会忽略前缀为的文件夹.
,所以例如一个名为.hidden
的文件夹将不会被提供。
要手动隐藏文件或文件夹,可以在static
之前插入自己的中间件,以在请求到达之前过滤出path。 以下内容将阻止从hidden
文件夹中提供任何文件:
app.use(function(req, res, next) { if (/\/hidden\/*/.test(req.path)) { return res.send(404, "Not Found"); // or 403, etc }; next(); }); app.use(express.static(__dirname+"/public"));
如果您希望在不使用Express的情况下获得解决scheme(如您明确要求的“简单”),请查看节点静态模块。
它允许您像Express的适当的中间件一样提供文件夹,但是它也允许您只提供特定的文件。
最简单的情况就是:
var http = require('http'), static = require('node-static'); var folder = new(static.Server)('./foo'); http.createServer(function (req, res) { req.addListener('end', function () { folder.serve(req, res); }); }).listen(3000);
如果你需要一些例子,看看GitHub项目页面,其中有几个。
PS:你甚至可以在全局安装节点静态,只需要从你想要服务的文件夹中的shell中运行它,就可以把它用作CLI工具:
$ static
而已 :-)!
PPS:关于你最初的例子,在这里使用pipe道stream而不是以同步的方式加载所有文件会更好。
正如在这个问题的接受答案中提到的,我build议使用http-server 。
它可以通过命令行启动,无需任何configuration
cd /path/to/directory http-server
我个人更喜欢从nginx的服务器文件(我也用它进行gzip编码,caching,SSL处理和负载平衡),节点只提供API。 也许不是你正在寻找的答案,但它提供了有趣的select。 也许你可以看看这个方法,并发现你喜欢它;)
如果你想要一个非常简单的方法,那么我想向你展示我的模块(它不仅适用于静态文件) simpleS ,使用npm install simples
simples来安装它。
把所有的文件放在一个文件夹中,例如files
。
这是魔法:
var simples = require('simples'); var server = simples(80); server.serve('files'); /* if you want to catch the acces to a folder and to do something, try this: server.serve('files', function (connection, files) { // Your application logic // For example show the files of the folder }); */
您不需要关心文件的内容types,它会自动从文件扩展名中检测它
我做了以下更改为AUTO-INCLUDE索引html中的文件。 因此,当您在文件夹中添加文件时,将自动从文件夹中选取文件,而不必将文件包含在index.html中
//// THIS WORKS FOR ME ///// in app.js or server.js var app = express(); app.use("/", express.static(__dirname)); var fs = require("fs"), function getFiles (dir, files_){ files_ = files_ || []; var files = fs.readdirSync(dir); for (var i in files){ var name = dir + '/' + files[i]; if (fs.statSync(name).isDirectory()){ getFiles(name, files_); } else { files_.push(name); } } return files_; } //// send the files in js folder as variable/array ejs = require('ejs'); res.render('index', { 'something':'something'........... jsfiles: jsfiles, }); ///-------------------------------------------------- ///////// in views/index.ejs --- the below code will list the files in index.ejs <% for(var i=0; i < jsfiles.length; i++) { %> <script src="<%= jsfiles[i] %>"></script> <% } %>
- 如何在callback函数中使用express.send(例如nodejs中的mysqljs使用)
- 为什么在使用Passport / Node Express的OAuth2Strategy进行身份validation时遇到TokenError?
- Node.js Express App – Rickshaw.Graph.RangeSlider TypeError:$(element).slider不是函数
- 将链接从移动网页浏览器迁移到移动应用程序
- 用于快速/连接的Ajax兼容中间件
- 单个或多个连接到MongoDB更好?
- nodejs +在不同的端口上expression奇怪的东西
- 我可以在没有–harmony标签的情况下运行Koajs吗?
- 如何使用monorepo(yarnpkg工作区)的webpack