使用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
}
相关推荐
学习使我快乐014 小时前
Express 学习
学习·node.js·express
Zeluar6 小时前
Node.js安装显示旧版本存在且无法覆盖
node.js
孟陬7 小时前
Node.js v26.0 新增超甜的语法糖 getOrInsert / getOrInsertComputed 介绍
python·node.js
淼_@淼7 小时前
node.js、node、nvm、npm、npx的关系
node.js
小粉粉hhh7 小时前
Node.js(四)——npm与包
前端·npm·node.js
网络点点滴1 天前
简述Node.js运行时核心架构
架构·node.js
小粉粉hhh1 天前
Node.js(三)——模块化
node.js
晓杰'1 天前
从0到1实现 Balatro 游戏后端(1):项目规划与牌型判断实现
后端·websocket·typescript·node.js·游戏开发·项目实战·nestjs
@PHARAOH1 天前
WHAT - npm和corepack
前端·npm·node.js
MPGWJPMTJT1 天前
从 Volta 迁移到 mise:Windows 下 Node 版本管理切换记录
前端·node.js