使用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
}
相关推荐
明月_清风10 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
guslegend14 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
zReadonly16 小时前
不用反复 nvm use 了:nvm-windows 2.x 按项目自动切换 Node.js
前端·node.js
百度一下吧20 小时前
Fastify 实战教程:从零搭建电商平台后台服务系统
node.js
YIAN20 小时前
LangGraph 完全入门指南:从线性工作流到带中断恢复的有状态 Agent 编排
langchain·node.js·agent
DevUp21 小时前
Directus 换掉 GPL 之后,Node.js CMS 到底怎么选?(二)
开源·node.js·cms
濮水大叔21 小时前
AI 编码实战:一句提示词,实现 CRUD、SSR 与多任务
node.js·ai编程·全栈
yume_sibai1 天前
01-Node.js环境安装与基础
node.js
damoluomu1 天前
Directus 换掉 GPL 之后,Node.js CMS 到底怎么选?(二)
node.js