Express JS发送文件数组和json作为响应

时间:2022-03-17 17:11:10

I am trying to send back to the client an array of files, and a few more properties which are in json format as one response when a route is hit. I am relatively new to Node and Express but I have not found anyway to handle this. I know (and have successfully tried) sending one file back to the client. The kind of response I want to send back should look like this

我试图向客户端发送一个文件数组,以及一些json格式的属性作为路由被击中时的一个响应。我对Node和Express比较陌生,但我还没有找到处理它的方法。我知道(并且已成功尝试)将一个文件发送回客户端。我想发回的那种回复应该是这样的

res.send([
          {
            name:'Floral dresses',
            date_added:'2016-10-11 06:52:39',
            designer:'Victoria',
            cover_photo: path.join(__dirname, '/images', '/clothes', '/cloth1.jpg'),
            photos: [
              path.join(__dirname, '/images', '/clothes', '/cloth1.jpg'),
              path.join(__dirname, '/images', '/clothes', '/cloth2.jpg'),
              path.join(__dirname, '/images', '/clothes', '/cloth3.jpg')
            ]
          },
          {
            name:'Vintage Dresses',
            date_added:'2016-02-08 18:12:09',
            designer:'Victoria',
            cover_photo: path.join(__dirname, '/images', '/clothes', '/cloth1.jpg'),
            photos: [
              path.join(__dirname, '/images', '/clothes', '/cloth1.jpg'),
              path.join(__dirname, '/images', '/clothes', '/cloth2.jpg'),
              path.join(__dirname, '/images', '/clothes', '/cloth3.jpg')
            ]
          }
        ];

cover_photo and photos are images saved on the file system.

cover_photo和照片是保存在文件系统上的图像。

How can I achieve this in Node JS?

我怎样才能在Node JS中实现这一目标?

1 个解决方案

#1


1  

Since JSON isn't great to transfer (a lot of) binary data, I would suggest returning URL's to the image files, and having them served by Express.

由于JSON不太适合传输(大量)二进制数据,我建议将URL返回到图像文件,并让它们由Express提供。

Taking your directory setup, you'd add a static file handler:

进行目录设置,您将添加一个静态文件处理程序:

app.use('/images', express.static(path.join(__dirname, 'images')));

For your JSON, the easiest then would be to pass the paths (relative to the website's root) to the image files, so the client can download them (or build a full URL and serve them in HTML).

对于您的JSON,最简单的方法是将路径(相对于网站的根目录)传递给图像文件,以便客户端可以下载它们(或构建完整的URL并以HTML格式提供它们)。

For instance:

cover_photo: '/images/clothes/cloth1.jpg'

#1


1  

Since JSON isn't great to transfer (a lot of) binary data, I would suggest returning URL's to the image files, and having them served by Express.

由于JSON不太适合传输(大量)二进制数据,我建议将URL返回到图像文件,并让它们由Express提供。

Taking your directory setup, you'd add a static file handler:

进行目录设置,您将添加一个静态文件处理程序:

app.use('/images', express.static(path.join(__dirname, 'images')));

For your JSON, the easiest then would be to pass the paths (relative to the website's root) to the image files, so the client can download them (or build a full URL and serve them in HTML).

对于您的JSON,最简单的方法是将路径(相对于网站的根目录)传递给图像文件,以便客户端可以下载它们(或构建完整的URL并以HTML格式提供它们)。

For instance:

cover_photo: '/images/clothes/cloth1.jpg'