使用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 分钟前
Nodejs也能写Agent - 16.LangGraph篇 - 条件分支与循环
前端·后端·langchain·node.js
Goodbye5 小时前
Node.js `path` 与 `fs` 模块详解——从路径操作到异步流程控制
node.js
Dreamboat-L9 小时前
NVM的安装与使用(保姆级教程)
npm·node.js
拾年27511 小时前
RAG 灵魂三问:你的文档是怎么"喂"给大模型的?
langchain·node.js·llm
南方程序猴13 小时前
Codex CLI + VS Code 组合教程:从 Node.js 安装到接入国内中转站
gpt·ai·chatgpt·node.js·ai编程
先吃饱再说13 小时前
后端也能用 jQuery 爬网页?Cheerio 了解一下
爬虫·node.js·jquery
qq_2128389714 小时前
后端Node.js + MySQL,前端Html+js,Nginx反向代理快速部署WEB项目
前端·mysql·node.js
徐小超14 小时前
从0到1落地AI知识问答系统(一):AI结对协作实战技巧
vue.js·node.js·全栈
snow@li1 天前
Node.js:在前端项目中的全景应用指南 / 为什么前端需要它 / 本地开发 vs 生产环境
前端·node.js
张人玉1 天前
基于 Vue2 + ECharts + Express + SQLite 的南阳市专题数据可视化系统
sqlite·echarts·express