使用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
}
相关推荐
__zRainy__14 分钟前
Node系列 · 数据库:单表查询
数据库·后端·mysql·node.js
薛定谔的算法2 小时前
NestJS:让 Node.js 后端告别「野路子」
后端·node.js·nestjs
zzx2006__3 小时前
node.js
node.js
GitLqr18 小时前
2026 Bun 全新姿态:从 Zig 到 Rust 的“暴力”重构与生态大爆发
rust·node.js·bun
赖龙3 天前
pnpm vs npm
前端·npm·node.js
烂蜻蜓3 天前
Node.js入门教程(三十一):Express 框架
node.js·express
小屁孩你滑稽掉了3 天前
prisma操作数据库的方法使用教程(简洁版)
数据库·node.js·prisma·fastify
__zRainy__3 天前
Node系列 · Node基础:https 模块
后端·网络协议·http·https·node.js
__zRainy__4 天前
Node.js Web 框架选型指南:从 Express 到 Hono 的全景对比
前端·node.js·express·koa·nestjs·egg·fastify
烂蜻蜓4 天前
Node.js入门教程(二十八):GET/POST请求
node.js