提供图像文件夹的最佳方式是什么?快递服务器

What is the best way to serve images folder? Express server

我为一个网站做了一个管理面板。

管理员使用 multer 上传数据和图片到服务器。

然后我想将这些图像提供给前端。用作静态文件使下载图像而不是显示,因此 url 在 html.

中不显示任何内容

我找到了解决方案,但我认为这不是正确的解决方案。因为有点慢。

信息:

每张图片的大小:2-3 MB。

Public 面 PHP -> 每张图像需要 15 秒

vue js 中的管理端前端 -> 每个图像需要 6 秒,加载需要 50 秒 20 张图片

Api 在快递服务器 Ubuntu 16.04.3 x64 尺寸 1 个 vCPU 2GB / 25GB 磁盘

解决方案:

router.get('/photos', async (req,res) => {
    let path = req.query.path;
    path = `../../uploads/${path}`
    if(path){
        fs.readFile(resolve(__dirname, path), function (err, content) {
            if (err) {
                res.writeHead(400, {'Content-type':'text/html'})
                console.log(err);
                res.end("No such image");    
            } else {
                //specify the content type in the response will be an image
                res.writeHead(200,{'Content-type':'image/jpg'});
                res.end(content);
            }
        });
    }
})`

您可以将文件夹作为 -

将以下内容添加到您的 app.js 文件中以进行表达。在这里,文件夹 my_images 与项目中的 app.js 存在于同一级别。

app.use(
  "/photos",
  express.static(path.join(__dirname, "my_images/"))
)

查看 express docs