javascript - Express serves static files that are not in root directory
问题描述
I have a website with a lot of HD videos so I want to put the videos files outside of web root directory.
Here is my web root directory:
/var/node/myapp
For some basic static files like javascript, css... I put them in public directory.
/var/node/myapp/public
For video files I want to put here
/hdd/videos
This is my current serve static code:
app.use(serveStatic(path.join(__dirname, 'public'), {
maxAge: keys.conf.maxAge,
etag: true,
setHeaders: setCustomCacheControl
}));
function setCustomCacheControl (res, path) {
if (serveStatic.mime.lookup(path) === 'text/html') {
res.setHeader('Cache-Control', 'public, max-age=0')
}
}
解决方案
您可以设置多个静态目录。例子:
app.use(express.static('public', {etag: true, maxAge: keys.conf.maxAge}));
app.use(express.static('/hdd/videos'));
但是,您提供给 express.static 函数的路径是相对于您启动节点进程的目录的。如果您从另一个目录运行 express 应用程序,则使用您要服务的目录的绝对路径会更安全。
快速静态文件的文档在这里
推荐阅读
- javascript - 使用数组重新选择导致组件始终呈现
- python - 如何对 svm 使用网格搜索?
- ruby-on-rails - 如何将用户对表单的响应保存到数据库
- git - 当我切换回无更改分支时,VS 会重建项目。如何跟踪构建文件但不推送它们?
- node.js - 在实时应用程序中使用 socket.io 接收数据
- javascript - 如何从另一个函数调用 Express 函数?
- asp.net-core-tag-helpers - 是否可以在不修改 _ViewImports.cshtml 文件的情况下覆盖自定义 TagHelper?
- python - 使用 randint 和 while 循环模拟掷骰子
- artifactory - Bintray 是否实现了 artifactory api?
- javascript - Array.reduce 和递归