使用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
}
相关推荐
万敏1 小时前
Vue3 全栈实战第七周:错误处理与请求层封装实战记录
vue.js·node.js·全栈
AD_youyu3 小时前
Node.js 安装教程
node.js
m0_3807438714 小时前
为 OpenAI 兼容接口配置教程
开发语言·python·node.js
__zRainy__18 小时前
Node系列 · ORM:Sequelize 简介
数据库·后端·node.js
水獭比特18 小时前
工具都批准了,为什么还不能执行?给 Agent 补上第二道校验
javascript·人工智能·node.js
__zRainy__1 天前
Node系列 · ORM:mysql 驱动程序
数据库·后端·mysql·node.js·orm
__zRainy__1 天前
Node系列 · ORM:Sequelize 模型
数据库·后端·node.js
秋秋小事1 天前
提示词工程
node.js
__zRainy__1 天前
Node系列 · 数据库:MySQL 安装
数据库·后端·mysql·node.js
EricStone1 天前
Agent开发学习一:Hello-Agents TypeScript 全栈实现
typescript·node.js·agent