使用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
}
相关推荐
怣疯knight4 小时前
Termux 运行 Node.js 实操记录(精简版)
node.js
AiSchoober10 小时前
schoober-ai-sdk:核心ReAct 引擎的实现
人工智能·ai·node.js·agent·ai编程
李子焱11 小时前
第三节:开发环境搭建与Trae IDE深度配置
前端·ide·python·node.js·trae ide
吴声子夜歌11 小时前
Node.js——Buffer
node.js
吴声子夜歌13 小时前
Node.js——调试器
node.js
吴声子夜歌13 小时前
Node.js——crypto加密模块
node.js·编辑器·vim
吴声子夜歌13 小时前
Node.js——assert断言模块
node.js
吴声子夜歌14 小时前
Node.js——domain模块处理错误(不推荐)
node.js
吴声子夜歌1 天前
Node.js——操作MySQL数据库
数据库·mysql·node.js
清风细雨_林木木1 天前
Node.js 和 Python 的关系
node.js