node.js 使用 restify 提供静态文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19281654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serving static files with restify
提问by Node Newbie
I am learning to use Node.js. Currently, I have a folder structure that looks like the following:
我正在学习使用 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 is my webserver code. I run this from a command-line using node server.js. The contents of that file are:
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);
});
As you can see, this server relies on RESTIFY. I've been told I must use RESTIFY. However, I can't figure out how to serve static files. For instance, how do I server the *.html, *.css, *.png, and *.js files in my app?
如您所见,此服务器依赖于 RESTIFY。有人告诉我必须使用 RESTIFY。但是,我不知道如何提供静态文件。例如,我如何在我的应用程序中为 *.html、*.css、*.png 和 *.js 文件提供服务?
Thank you!
谢谢!
回答by vp_arth
From the documentation:
从文档:
server.get(/\/docs\/public\/?.*/, restify.serveStatic({
directory: './public'
}));
But this will search files in the ./public/docs/public/directory.
I prefer to use __dirnamekey here:
但这将搜索目录中的./public/docs/public/文件。
我更喜欢在这里使用__dirname键:
server.get(/\/public\/?.*/, restify.serveStatic({
directory: __dirname
}));
The value of __dirnameis equal to script file directory path, which assumed to be also a folder, where is publicdirectory.
的值__dirname等于脚本文件目录路径,假设也是一个文件夹,其中是public目录。
And now we map all /public/.*urls to ./public/directory.
现在我们将所有/public/.*url映射到./public/目录。
回答by Aditya Kresna Permana
According to my current restify version (v5.2.0)
根据我目前的restify版本(v5.2.0)
the serveStatichas been moved into plugins, so the code would be like this
在serveStatic已移入plugins,所以代码会是这样
server.get(
/\/(.*)?.*/,
restify.plugins.serveStatic({
directory: './static',
})
)
Syntax above will serve your static files on folder static. So you can get the static file like http://yoursite.com/awesome-photo.jpg
上面的语法将为您的文件夹中的静态文件提供服务static。所以你可以得到像这样的静态文件http://yoursite.com/awesome-photo.jpg
For some reason if you want to serve the static files under specific path like this http://yoursite.com/assets/awesome-photo.jpgfor example.
例如,出于某种原因,如果您想在特定路径下提供静态文件http://yoursite.com/assets/awesome-photo.jpg。
The code should be refactored into this
代码应该重构为这个
server.get(
/\/assets\/(.*)?.*/,
restify.plugins.serveStatic({
directory: `${app_root}/static`,
appendRequestPath: false
})
)
The option appendRequestPath: falseabove means we dont include assetspath into the file name
appendRequestPath: false上面的选项意味着我们不将assets路径包含在文件名中
回答by leo
From Restify 7 the routes no longer take full regexes, so if you want /public/stylesheet.cssto serve the file ./public/stylesheet.css, your code would now look like this:
从 Restify 7 开始,路由不再使用完整的正则表达式,所以如果你想/public/stylesheet.css提供文件./public/stylesheet.css,你的代码现在看起来像这样:
server.get('/public/*', restify.plugins.serveStatic({
directory: __dirname,
}))
This is because Restify 7 has a new (potentially faster) routing back-end: find-my-way
这是因为 Restify 7 有一个新的(可能更快)路由后端:find-my-way
回答by Zohaib
this is how i'm serving static files in restify
这就是我在 restify 中提供静态文件的方式
server.get(/\/public\/docs\/?.*/, restify.plugins.serveStatic({
directory: __dirname,
default: 'index.html'
}));
public access path will be : example.com/public/docs/index.html
公共访问路径将是:example.com/public/docs/index.html
回答by Mike Fleming
I came across this issue just recently, so while this may not help you it could help others who are having trouble with it.
我最近才遇到这个问题,所以虽然这可能对你没有帮助,但可以帮助其他遇到问题的人。
When you declare Restify as const restify = require('restify');, the serveStatic method will be in the plugins object so using restify.serveStaticwill quietly fail. The correct way to access the method is restify.plugins.serveStatic.
当您const restify = require('restify');将 Restify声明为 时, serveStatic 方法将位于插件对象中,因此使用restify.serveStatic将悄悄失败。访问该方法的正确方法是restify.plugins.serveStatic。
You can find the update docs here: http://restify.com/docs/plugins-api/#serve-static
您可以在此处找到更新文档:http: //restify.com/docs/plugins-api/#serve-static
回答by techyaura
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();
});
});
回答by Om Sharma
Try this : Here view is a static resource directory name
试试这个:这里的视图是一个静态资源目录名称
server.get('/\/.*/', restify.plugins.serveStatic({
directory: __dirname + "/view/",
default: './home.html'
})
);

