使用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
}
相关推荐
八角丶12 小时前
Node.js Cluster 详解
前端·node.js
濮水大叔15 小时前
CabloyJS 强大之处不仅仅是 IoC,而是全栈资源的寻址体系
typescript·node.js·全栈
烂蜻蜓16 小时前
Node.js入门教程(二十六):内置模块
node.js
东方小月16 小时前
从零开发一个 Coding Agent(十一):实现 CLI 的 print 模式
node.js·全栈
__zRainy__17 小时前
Node系列 · Node基础:ES 模块化
node.js
FungLeo18 小时前
Node 后端实战 · 后端敏感数据怎么防泄露?PII 自动脱敏与审计日志实战
node.js·数据脱敏·数据防泄漏·pii 脱敏
深念Y18 小时前
Opencode Event 表写入优化方案
数据库·人工智能·ai·node.js·bug·优化·opencode
ikun778g20 小时前
DeepSeek Harness 本地部署保姆级教程:从 Node.js 24.0.0 安装到 WorkBuddy 一键运行
ai·node.js
烂蜻蜓1 天前
Node.js入门教程(二十三):全局对象
node.js·编辑器·vim
xywww1682 天前
Node.js Claude API 实战接入:SDK 调用 Opus 5、环境变量配置与报错排查
node.js