Created
January 6, 2020 17:41
-
-
Save julianrubisch/2e125b428aa188ef8a60841afc98e90d to your computer and use it in GitHub Desktop.
express js asset proxy route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get( | |
"/:asset_type/:space_id/:asset_id/:token/:name", | |
async (req, res, next) => { | |
const format = req.query.fm; | |
const width = req.query.w; | |
const { asset_type, space_id, asset_id, token, name } = req.params; | |
res.locals.fetchUrl = `https://${asset_type}.ctfassets.net/${space_id}/${asset_id}/${token}/${name}?fm=${format}${ | |
width ? `&w=${width}` : "" | |
}`; | |
res.locals.cacheKey = `${token}/${name}/${format}/${width}`; | |
next(); | |
}, | |
fileCacheMiddleware({ cacheDir, logger }), | |
(req, res) => { | |
res.set({ | |
"Content-Type": res.locals.contentType, | |
"Content-Length": res.locals.contentLength | |
}); | |
res.end(res.locals.buffer, "binary"); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment