javascript express.static 与 res.sendFile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31425284/
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
express.static vs. res.sendFile
提问by Mika
What's the difference and which should I use? My goal is to simply serve static html pages and files.
有什么区别,我应该使用哪个?我的目标是简单地提供静态 html 页面和文件。
router.use('/', express.static(path.resolve(public + '/index.html')))
or
或者
router.get('/', function(req, res) {
res.sendFile(path.resolve(public + '/index.html'))
})
回答by kyrisu
Static middleware and sendFile() are mostly the same - they both pipe the file stream to response stream.
静态中间件和 sendFile() 大致相同——它们都将文件流通过管道传输到响应流。
The difference is that express.static will:
不同之处在于 express.static 将:
- set ETagfor you
- allow you to set extension fallbacks (for example html -> htm)
- 为你设置ETag
- 允许您设置扩展回退(例如 html -> htm)
sendFile on the other hand will:
另一方面,sendFile 将:
- set the Content-Type response HTTP header based on file extension
- 根据文件扩展名设置 Content-Type 响应 HTTP 标头
They both will:
他们都会:
- set max-age property on Cache-Control
- set Last-Modified header
- allow you to set any other headers through options object
- allow you to ignore dotfiles
- 在 Cache-Control 上设置 max-age 属性
- 设置 Last-Modified 标头
- 允许您通过选项对象设置任何其他标题
- 允许您忽略点文件
The main advantage of using static middleware is that you don't need to write specific route for every file separately (or sanitize parameters) but just point the middleware to the right directory.
使用静态中间件的主要优点是您不需要为每个文件单独编写特定的路由(或清理参数),而只需将中间件指向正确的目录。
回答by Stuart P. Bentley
If you want to serve any files from your public
directory, you should use the express.static
middleware to serve the entire directory, mounted to your app root.
如果你想从你的public
目录提供任何文件,你应该使用express.static
中间件来提供整个目录,挂载到你的应用程序根目录。
(Also, you may wish to consider including the static serving middleware as a dependency of your project, as serve-static
, so that it may update independently of Express.)
(此外,您可能希望考虑将静态服务中间件作为您项目的依赖项 as serve-static
,以便它可以独立于 Express 进行更新。)
var serveStatic = require('serve-static'); // same as express.static
/* ... app initialization stuff goes here ... */
router.use(serveStatic(public)); // assuming you've defined `public` to some path above
This will respond to requests for files by sending the files, reading index.html
files for responding to requests for directory roots.
这将通过发送文件、读取index.html
文件以响应对目录根的请求来响应对文件的请求。
If, however, you have some kind of complex logic in your route (or you may at some point in the future), thenyou should use sendFile
. For example, for a server that sends a different favicon every minute:
但是,如果您的路线中有某种复杂的逻辑(或者您可能在将来的某个时候),那么您应该使用sendFile
. 例如,对于每分钟发送不同网站图标的服务器:
router.get('/favicon.ico', function(req, res) {
return res.sendFile(path.resolve(public, '/icons/' + new Date().getMinutes() + '.ico'));
})