如何在node.js中使用ejs从特定文件夹中使用每个图像?

我想从一个特定的文件夹中获取每个图像,并在一个页面上使用它。
图像具有随机名称,但现在只有jpg格式,将来可能会改变。

我可以用ejs来做吗,还是必须使用jQuery?

使用最新的JavaScript语法(可能需要babel )。 另外请确保你安装koa@2不是旧的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);