使用Express Js提供目录图像 [英] Serving directory images using Express Js

查看:55
本文介绍了使用Express Js提供目录图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序用例;用户可以将图像上传到服务器中存储在目录中的图像,并且用户可以查看所有上传和存储在该目录中的图像.

I have a simple use case for an application; a user can upload images to server where they are stored in a directory, and a user can view all images uploaded and stored in this directory.

第一个条件的功能正在运行,并且应用程序将上传的图像持久保存到图像目录中.

The functionality for the first criteria is working, and the application persists uploaded images to the image directory.

下一步是返回所有图像.对于前端,我将使用某种瓷砖库将图像呈现给用户.但是,我的问题与如何通过express js最好地将所有图像"返回给最终用户有关.

The next step is to return all images. For the front end, i will use some kind of tile gallery to render images to the user. However, my question is related to how best to return "all images" to the end user, through express js.

从本质上讲,我希望进行目录转储,但是对于如何执行此操作的优雅(阅读:任何!)解决方案感到困惑.任何意见或建议,最赞赏.

Essentially, I am hoping to do a directory dump, but am confused as to an elegant (read: any!) solution on how to do this. Any advice or suggestion most appreciated.

为澄清起见,路线在命中时应返回所有图像.由于将来的实现将根据某些条件过滤图像.

to clarify, the route should return all images when hit. As future implementations will filter images based on certain criteria.

示例:

    // return the view page for register
app.get('/gallery',function(req,res){
      // return images here
      res.send();
});

推荐答案

如果我对您的理解正确,那么您要做的就是静态地为文件夹提供服务.为此,您可以使用

If I understood you correctly, all you need to do is to statically serve a folder. For that, you can use

app.use('/images', express.static('path/to/images'))

这篇关于使用Express Js提供目录图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆