使用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
}
相关推荐
星栈7 小时前
我以为 TS7.0 只是换个版本号,结果编译快了 9 倍,也踩了 5 个坑
后端·typescript·node.js
Synmbrf7 小时前
Mosquitto 持久化消息积压导致网关内存耗尽的排查
node.js
GISHUB7 小时前
Express + TypeScript + ESM 后端框架示例(@yao-pkg/pkg 打包)
typescript·express
AINative软件工程9 小时前
LLM Streaming 背压工程实践:慢客户端、断流重连与服务端缓冲区的生产设计
node.js·llm
码林鼠21 小时前
webpack的基本配置
前端·webpack·node.js
FeelTouch Labs1 天前
Node.js 中的 spawn 和 exec:子进程执行的不同方式对比
node.js·编辑器·vue·vim
烂蜻蜓1 天前
Node.js入门教程(一):初识Node.js——服务端JavaScript的诞生与特点
开发语言·javascript·node.js
凌云拓界1 天前
NodeVerdict:Node.js 原生诊断数据可视化工具
信息可视化·架构·typescript·开源·node.js·github·bug
用户2930750976691 天前
React Router v6 实战:理解 SPA 路由系统
node.js