Reputation: 146
I want to take every image from a specific folder and use it on a page.
The images have random names but only jpg format for now which may change in the future.
Can I do that with ejs or do I have to use jQuery?
Upvotes: 1
Views: 195
Reputation: 6378
Using latest JavaScript syntax (probably need babel
). Also make sure you install koa@2
not the old koa
.
import {readFile} from 'fs-promise';
import listFilepaths from 'list-filepaths';
import Koa from 'koa';
const app = new Koa();
app.use(async (ctx) => {
if (ctx.request.querystring.indexOf('.jpg')>0) {
const fname = ctx.request.querystring.split('=')[1];
ctx.body = await readFile(`images/${fname}`);
} else {
let images = await listFilepaths('./images',{relative:true});
images = images.map(i=>i.replace('images/',''));
ctx.body = `${images.map(i=>`<img src="/?i=${i}"/>`)}`;
}
});
app.listen(3000);
Upvotes: 1