使用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 分钟前
Tube - Video Reactions
react.js·node.js·next.js
折七4 小时前
2026 年 Node.js 后端技术选型,为什么我选了 Hono 而不是 NestJS
前端·后端·node.js
下北沢美食家4 小时前
Express框架入门
开发语言·javascript·express
我叫唧唧波8 小时前
【NodeJS】从入门到进阶
node.js
张3蜂8 小时前
Node.js 安装与配置完全指南:从零开始搭建开发环境
node.js
礼拜天没时间.1 天前
Node.js运维部署实战:从0到1开始搭建Node.js运行环境
linux·运维·后端·centos·node.js·sre
等什么君!1 天前
如何正确使用nvm工具管理 node.js
node.js
受打击无法动弹2 天前
Window 10部署openclaw报错node.exe : npm error code 128
npm·node.js·openclaw
全马必破三3 天前
Webpack知识点汇总
前端·webpack·node.js
NEXT063 天前
CommonJS 与 ES Modules的区别
前端·面试·node.js