使用Node.js和Express搭建图片缓存服务或应用服务器

使用Node.js和Express生成缩略图:
复制代码
const express = require('express');
const sharp = require('sharp');
const app = express();
const port = 3000;
 
app.get('/thumbnail/:filename', async (req, res) => {
    const filename = req.params.filename;
    const imagePath = `/path/to/images/${filename}`;
    sharp(imagePath)
        .resize(100, 100) // 设置缩略图尺寸
        .toBuffer()
        .then(data => {
            res.type('jpeg'); // 根据需要设置正确的MIME类型
            res.send(data);
        })
        .catch(err => {
            console.error(err);
            res.status(500).send('Error generating thumbnail');
        });
});
 
app.listen(port, () => {
    console.log(`Thumbnail server running at http://localhost:${port}`);
});

配置nginx反向代理

复制代码
location /thumbnail/ {
    proxy_pass http://localhost:3000/; # Node.js应用地址
    proxy_set_header Host $host; # 传递主机头信息到后端服务器
    proxy_set_header X-Real-IP $remote_addr; # 传递客户端IP地址到后端服务器
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 传递X
}
相关推荐
*小雪2 小时前
nvm的安装与管理和npm audit的报错解决
前端·npm·node.js
xinhuanjieyi3 小时前
将 Node.js 安装到 /ext 目录的办法
node.js
weixin_427771613 小时前
npm 绕过2FA验证
前端·npm·node.js
*小天屎*3 小时前
UE5 项目打包与 Pixel Streaming 浏览器部署指南
ue5·node.js·虚幻引擎
方方洛1 天前
技术实践总结:schema-bridgion:json、xml、yaml、toml文件相互转换
xml·前端·typescript·node.js·json
心.c1 天前
Vue3+Node.js实现文件上传并发控制与安全防线 进阶篇
前端·javascript·vue.js·安全·node.js
【赫兹威客】浩哥2 天前
【赫兹威客】Node.js安装教程
node.js
晨欣2 天前
pnpm vs npm 命令对照表
前端·npm·node.js
奔跑的web.2 天前
npm install发生了什么?
前端·npm·node.js
小二李2 天前
Node.js工程师养成计划
node.js