使用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
}
相关推荐
味悲几秒前
CVE-2025-55182 (React2Shell) 漏洞分析与复现
安全·node.js
Czzzzlq4 小时前
【无标题】
typescript·node.js·ai编程
前端若水6 小时前
开发环境准备:Python、Node.js、Docker与Git
python·docker·node.js
机器视觉知识推荐、就业指导6 小时前
npm 安装/运行报错及解决方案
前端·npm·node.js
独泪了无痕6 小时前
pnpm依赖管理:从零开始的实践手册
前端·npm·node.js
Bolt21 小时前
用 pnpm 11 省掉项目里的 .nvmrc 与 .npmrc
前端·npm·node.js
学习使我快乐011 天前
Express 学习
学习·node.js·express
Zeluar1 天前
Node.js安装显示旧版本存在且无法覆盖
node.js
孟陬1 天前
Node.js v26.0 新增超甜的语法糖 getOrInsert / getOrInsertComputed 介绍
python·node.js
淼_@淼1 天前
node.js、node、nvm、npm、npx的关系
node.js