unpkg/modules/actions/serveStaticFile.js

27 lines
686 B
JavaScript
Raw Normal View History

2018-12-17 17:38:05 +00:00
const path = require('path');
const etag = require('etag');
2018-12-17 17:38:05 +00:00
const getContentTypeHeader = require('../utils/getContentTypeHeader');
function serveStaticFile(req, res) {
2018-12-17 17:38:05 +00:00
const tags = ['file'];
const ext = path.extname(req.entry.name).substr(1);
if (ext) {
tags.push(`${ext}-file`);
}
res
.set({
2018-12-17 17:38:05 +00:00
'Content-Length': req.entry.size,
'Content-Type': getContentTypeHeader(req.entry.contentType),
'Cache-Control': 'public, max-age=31536000, immutable', // 1 year
'Last-Modified': req.entry.lastModified,
ETag: etag(req.entry.content),
2018-12-17 17:38:05 +00:00
'Cache-Tag': tags.join(',')
})
.send(req.entry.content);
}
module.exports = serveStaticFile;