使用 restify 提供静态文件
Posted
技术标签:
【中文标题】使用 restify 提供静态文件【英文标题】:Serving static files with restify 【发布时间】:2013-10-17 09:07:23 【问题描述】:我正在学习使用 Node.js。目前,我的文件夹结构如下所示:
index.html
server.js
client
index.html
subs
index.html
page.html
res
css
style.css
img
profile.png
js
page.js
jquery.min.js
server.js 是我的网络服务器代码。我使用node server.js
从命令行运行它。该文件的内容是:
var restify = require('restify');
var server = restify.createServer(
name: 'Test App',
version: '1.0.0'
);
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.get('/echo/:name', function (req, res, next)
res.send(req.params);
return next();
);
server.listen(2000, function ()
console.log('%s running on %s', server.name, server.url);
);
如您所见,此服务器依赖于 RESTIFY。有人告诉我必须使用 RESTIFY。但是,我不知道如何提供静态文件。例如,如何在我的应用程序中提供 *.html、*.css、*.png 和 *.js 文件?
谢谢!
【问题讨论】:
可能重复:***.com/questions/15463841/… 【参考方案1】:来自documentation:
server.get(/\/docs\/public\/?.*/, restify.plugins.serveStatic(
directory: './public'
));
但这会搜索./public/docs/public/
目录中的文件。
如果您不想将请求路径附加到它,请使用appendRequestPath: false
选项。
我更喜欢在这里使用__dirname 键:
server.get(/\/public\/?.*/, restify.plugins.serveStatic(
directory: __dirname
));
__dirname
的值等于脚本文件目录路径,假设也是一个文件夹,这里是public
目录。
现在我们将所有/public/.*
url 映射到./public/
目录。
现在还存在serveStaticFiles插件:
server.get('/public/*', // don't forget the `/*`
restify.plugins.serveStaticFiles('./doc/v1')
); // GET /public/index.html -> ./doc/v1/index.html file
【讨论】:
__dirname
在你的情况下会有什么价值?【参考方案2】:
从 Restify 7 开始,路由 no longer take full regexes,因此如果您希望 /public/stylesheet.css
为文件 ./public/stylesheet.css
提供服务,您的代码现在将如下所示:
server.get('/public/*', restify.plugins.serveStatic(
directory: __dirname,
))
这是因为 Restify 7 有一个新的(可能更快的)路由后端:find-my-way
【讨论】:
【参考方案3】:这就是我在 restify 中提供静态文件的方式
server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic(
directory: __dirname,
default: 'index.html'
));
公共访问路径将是:example.com/public/docs/index.html
【讨论】:
【参考方案4】:试试这个:这里的视图是一个静态资源目录名
server.get('/\/.*/', restify.plugins.serveStatic(
directory: __dirname + "/view/",
default: './home.html'
)
);
【讨论】:
【参考方案5】:server.get('/', function(req, res, next)
fs.readFile(__dirname + '/index.html', function (err, data)
if (err)
next(err);
return;
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(data);
next();
);
);
【讨论】:
如何导入fs?【参考方案6】:根据我目前的restify版本(v5.2.0)
serveStatic
已移至plugins
,所以代码是这样的
server.get(
/\/(.*)?.*/,
restify.plugins.serveStatic(
directory: './static',
)
)
上面的语法将在文件夹static
上提供您的静态文件。所以你可以得到像http://yoursite.com/awesome-photo.jpg
这样的静态文件
出于某种原因,如果您想在特定路径下提供静态文件,例如 http://yoursite.com/assets/awesome-photo.jpg
。
代码应该重构成这样
server.get(
/\/assets\/(.*)?.*/,
restify.plugins.serveStatic(
directory: `$app_root/static`,
appendRequestPath: false
)
)
上面的选项appendRequestPath: false
表示我们不在文件名中包含assets
路径
【讨论】:
【参考方案7】:我最近才遇到这个问题,所以虽然这可能对您没有帮助,但它可以帮助遇到问题的其他人。
当您将 Restify 声明为 const restify = require('restify');
时,serveStatic 方法将在插件对象中,因此使用 restify.serveStatic
将悄悄地失败。访问方法的正确方法是restify.plugins.serveStatic
。
您可以在此处找到更新文档:http://restify.com/docs/plugins-api/#serve-static
【讨论】:
以上是关于使用 restify 提供静态文件的主要内容,如果未能解决你的问题,请参考以下文章
从 django 静态文件提供服务并上传到 heroku 时无法找到反应静态文件
在 Elasticbeanstalk Docker 环境中提供 Django 静态文件