使用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
}
相关推荐
忆想不到的晖13 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent
用户119770794807314 小时前
首饰、织物主图缩到 800×800 出摩尔纹?用 pica 在 Node 端对比 7 种缩放方式,量化假条纹和边缘光晕
node.js·canvas
HTouying20 小时前
NodeJS RocketMQ-5.5.0集群 rocketmq-client-nodejs客户端连接工具类
node.js·rocketmq
苦逼前端画 K 线2 天前
从L3测试牌照看自动驾驶分级与工程验证
网络安全·node.js·自动驾驶·网络技术·ai换脸·诈骗防范·l3
想不到一个好的ID2 天前
场景设计题目:点赞系统
后端·node.js
用户86570138282462 天前
定时任务重复执行了三次:从 setInterval 到分布式锁
node.js
*Snowgrass*2 天前
npm简要介绍
前端·npm·node.js
斯内普吖2 天前
(开源)校园资料分享平台实战指南 基于 Node.js + Express + Vue + MySQL
vue.js·mysql·开源·node.js·express
前端小D3 天前
Node.js实战
node.js
夕水3 天前
夕水文学网502 故障 —— 爬虫把 Node 堆内存吃爆
javascript·后端·node.js