Route for serving pictures

This commit is contained in:
Henri Bergius 2018-12-26 18:54:38 +01:00
parent d98042d301
commit 5e7c718e39
2 changed files with 23 additions and 0 deletions

View file

@ -5,12 +5,14 @@ const Cors = require('koa-cors');
const Mount = require('koa-mount');
const path = require('path');
const routePictures = require('./route/pictures');
const route35c3 = require('./route/35c3');
const app = new Koa();
const router = new Router();
route35c3(router);
routePictures(router);
app
.use(Cors())

21
server/route/pictures.js Normal file
View file

@ -0,0 +1,21 @@
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const fsStat = promisify(fs.stat);
const fsReadDir = promisify(fs.readdir);
module.exports = (router) => {
router.get('/pictures/:folder', async (ctx) => {
const folder = path.resolve(__dirname, `../../pictures/${path.basename(ctx.params.folder)}`);
try {
await fsStat(folder);
} catch (e) {
ctx.status = 404;
return ctx;
}
const files = await fsReadDir(folder);
ctx.body = files.map(f => `/pictures/${path.basename(ctx.params.folder)}/${f}`);
return ctx;
});
};