使用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
}
相关推荐
wxwx_bscxy3221 小时前
NodeJS 高校学业预警系统10551
mysql·node.js·vue·高校学业预警
FungLeo7 小时前
成为全栈·Node 后端篇·列表接口三件套:分页、筛选、排序
node.js·列表排序·列表分页·成为全栈·列表接口设计·列表筛选
晴天1610 小时前
浏览器中ESM与AMD模块共存的解决方案
前端·node.js
晴天1612 小时前
Node.js 模块化混合开发指南:CommonJS / ESM 混用适配方案与落地配置
node.js
FungLeo14 小时前
成为全栈·Node 后端篇·分类与标签:多对多关系的建模与查询
node.js·成为全栈·分类与标签·多对多关系建模·多对多关系查询
晴天1614 小时前
ES 标准、V8 引擎与 Node.js 版本联动关系全解与实战踩坑
大数据·elasticsearch·node.js
晴天1615 小时前
Vite vs Webpack 全方位对比
前端·webpack·node.js
且听风吟_xincell16 小时前
LibUV:Node.js 异步能力的底层支撑
node.js
掰头战士1 天前
从LLM到Agent、Agent的6大核心。这些基础知识你还记得吗
node.js·llm·agent
FungLeo1 天前
成为全栈·Node 后端篇·注册登录全流程实现
node.js·登录流程·成为全栈·注册流程