使用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
}
相关推荐
zhensherlock17 小时前
Protocol Launcher 系列:Overcast 一键订阅播客
前端·javascript·typescript·node.js·自动化·github·js
Bigger21 小时前
🚀 mini-cc:打造你的专属轻量级 AI 编程智能体
前端·node.js·claude
算是难了1 天前
TypeORM vs Prisma
数据库·typescript·node.js
xxjj998a1 天前
如何安装linux版本的node.js
linux·运维·node.js
青花瓷2 天前
Windows下Node.js的安装
node.js
暮雪倾风2 天前
【JS-Node】node.js环境安装及使用
开发语言·javascript·node.js
冴羽3 天前
请愿书:Node.js 核心代码不应该包含 AI 代码!
前端·javascript·node.js
太难了啊3 天前
从零构建你的 AI Agent 框架:Node.js 版 HelloAgents 实战指南
人工智能·node.js
网络点点滴3 天前
Node.js-构建模板
node.js
捉鸭子3 天前
海关总署瑞数vmp算法还原
python·网络安全·node.js·网络爬虫