使用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
}
相关推荐
yuanlaile20 小时前
从入门到部署|2026年Koa全栈开发实战:覆盖Node.js、数据库、部署与云架构全链路
微服务·云原生·kubernetes·node.js·serverless·nodejs全栈开发
onebyte8bits21 小时前
NestJS 系列教程(十八):文件上传与对象存储架构(Multer + S3/OSS + 访问控制)
前端·架构·node.js·状态模式·nestjs
苏瞳儿21 小时前
前端/后端-配置跨域
前端·javascript·node.js·vue
码云之上2 天前
上下文工程实战:解决多轮对话中的"上下文腐烂"问题
前端·node.js·agent
奔跑的呱呱牛2 天前
前端/Node.js操作Excel实战:使用@giszhc/xlsx(导入+导出全流程)
前端·node.js·excel·xlsx·sheetjs
Southern Wind2 天前
AI Skill Server 动态技能中台
前端·后端·mysql·node.js
米丘2 天前
Vite 代理跨域全解析:从 server.proxy 到请求转发的实现原理
javascript·node.js·vite
CyrusCJA2 天前
Nodejs自定义脚手架
javascript·node.js·js
AI视觉网奇2 天前
pnpm 安装笔记
node.js