Skip to content

Commit

Permalink
feat: add middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Sep 7, 2021
1 parent fd2bb50 commit 1bab12c
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
import superagent from 'superagent';

const getResource = (url) =>
new Promise((resolve, reject) => {
try {
superagent.get(url).end((err, result) => {
if (err) resolve(result || err.message);
else resolve(result);
});
} catch (err) {
reject(err);
}
});

export default function applyConfig(config) {
return config
const { staticFiles } = config.settings;
// EXAMPLE:
// config.settings.staticFiles = {
// 'favicon.ico': {
// url: favicon, // webpack loaded resource
// contentType: 'image/ico',
// },
// };

if (__SERVER__ && staticFiles) {
const express = require('express');

const staticResourcesMiddleware = express.Router();
staticResourcesMiddleware.id = 'volto-middleware-staticresources';
staticResourcesMiddleware.all('**', async (req, res, next) => {
Object.keys(staticFiles).forEach((f) => {
if (req.originalUrl.includes(f)) {
const url = staticFiles[f].url;
getResource(url)
.then((resource) => {
res.set('Content-Type', staticFiles[f].contentType);
res.set('Content-Disposition', `inline`);
res.send(resource.body);
})
.catch((err) => res.status(400).send(err));
}
});

next();
});

config.settings.expressMiddleware = [
...config.settings.expressMiddleware,
staticResourcesMiddleware,
];
}

return config;
}

0 comments on commit 1bab12c

Please sign in to comment.