使用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
}
相关推荐
苦逼前端画 K 线14 小时前
从L3测试牌照看自动驾驶分级与工程验证
网络安全·node.js·自动驾驶·网络技术·ai换脸·诈骗防范·l3
想不到一个好的ID14 小时前
场景设计题目:点赞系统
后端·node.js
用户865701382824616 小时前
定时任务重复执行了三次:从 setInterval 到分布式锁
node.js
*Snowgrass*18 小时前
npm简要介绍
前端·npm·node.js
斯内普吖18 小时前
(开源)校园资料分享平台实战指南 基于 Node.js + Express + Vue + MySQL
vue.js·mysql·开源·node.js·express
前端小D2 天前
Node.js实战
node.js
夕水2 天前
夕水文学网502 故障 —— 爬虫把 Node 堆内存吃爆
javascript·后端·node.js
SEO_juper2 天前
Node.js Product 结构化数据生成器:给产品页一键生成 JSON-LD
开发语言·前端·javascript·人工智能·node.js·json·1024程序员节
FungLeo3 天前
Swift macOS 游戏开发实战|空当接龙 06|使用 SVG 与 Node.js 制作 macOS 游戏牌面
macos·node.js·svg·swift·游戏素材
前端呆头鹅3 天前
5分钟深入掌握 Node.js 事件循环
node.js