使用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
}
相关推荐
子兮曰35 分钟前
浏览器与 Node.js 全局变量体系详解:从 window 到 global 的核心差异
前端·javascript·node.js
汤姆Tom20 小时前
Node.js 版本管理、NPM 命令、与 NVM 完全指南
前端·npm·node.js
RoyLin2 天前
TypeScript设计模式:原型模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:单例模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:工厂方法模式
前端·后端·node.js
RoyLin2 天前
TypeScript设计模式:模板方法模式
前端·后端·node.js
RoyLin3 天前
TypeScript设计模式:适配器模式
前端·后端·node.js
RoyLin3 天前
TypeScript设计模式:迭代器模式
javascript·后端·node.js
前端双越老师3 天前
2025 年还有前端不会 Nodejs ?
node.js·agent·全栈
人工智能训练师3 天前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js