使用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
}
相关推荐
小p18 小时前
nodejs学习6:nodejs应用的优雅退出
node.js
军哥全栈AI20 小时前
Windows11 彻底卸载Node.js(无残留,适配所有版本)
npm·node.js
困惑阿三20 小时前
全栈部署排雷手册:从 405 报错到飞书推送成功
服务器·前端·后端·nginx·阿里云·node.js·飞书
Andytoms21 小时前
Node.js 版本和 pnpm 版本的对应关系
node.js
坐吃山猪1 天前
NodeJS极简后端服务
node·express
头发多多程序媛2 天前
解决依赖下载报错,npm ERR! code EPERM
前端·npm·node.js
fanjinzhi2 天前
Node.js通用计算15--TypeScript介绍
javascript·typescript·node.js
light blue bird2 天前
MES/ERP的Web多页签报表系统
数据库·node.js·ai大数据·mes/erp·web报表
方寸猿2 天前
MindSharePCIe3.0-2 PCIe 体系结构概述- 2.1 PCI Express 简介-2.1.1 软件的后向兼容
express