使用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
}
相关推荐
zhensherlock21 小时前
Protocol Launcher 系列:1Writer iOS 上的 Markdown 文档管理
javascript·笔记·ios·typescript·node.js·iphone·ipad
吴声子夜歌21 小时前
Node.js——操作MongoDB
数据库·mongodb·node.js
吴声子夜歌1 天前
Node.js——dns模块
开发语言·node.js·php
吴声子夜歌1 天前
Node.js——zlib压缩模块
java·spring·node.js
"Wild dream"1 天前
NodeJs内置的Npm
前端·npm·node.js
摇滚侠1 天前
JAVA 项目教程《苍穹外卖-12》,微信小程序项目,前后端分离,从开发到部署
java·开发语言·vue.js·node.js
怣疯knight2 天前
Termux 运行 Node.js 实操记录(精简版)
node.js
AiSchoober2 天前
schoober-ai-sdk:核心ReAct 引擎的实现
人工智能·ai·node.js·agent·ai编程
李子焱2 天前
第三节:开发环境搭建与Trae IDE深度配置
前端·ide·python·node.js·trae ide