Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Created January 6, 2020 17:41
Show Gist options
  • Save julianrubisch/2e125b428aa188ef8a60841afc98e90d to your computer and use it in GitHub Desktop.
Save julianrubisch/2e125b428aa188ef8a60841afc98e90d to your computer and use it in GitHub Desktop.
express js asset proxy route
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