使用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
}
相关推荐
008爬虫实战录8 小时前
【码上爬】 题十二:如来神掌 困难, JSVMP加密,使用代理补环境
前端·javascript·node.js
008爬虫实战录12 小时前
【码上爬】 题九:webpack调试 堆栈分析
前端·webpack·node.js
xiaoxue..12 小时前
Node.js 笔试题讲解
后端·面试·node.js
小小前端仔LC13 小时前
Node.js + LangChain +React:搭建个人知识库(四)- 把向量和文件切块存入mysql中
后端·node.js
海上彼尚14 小时前
Nodejs也能写Agent - 7.基础篇 - MCP
前端·javascript·人工智能·node.js
海兰14 小时前
【实用程序】图片处理服务,前端应用 (Vue 3 + Pinia + Vite)后端服务 (Node.js + Express + Sharp)
前端·javascript·vue.js·node.js·pinia
海上彼尚15 小时前
Nodejs也能写Agent - 6.基础篇 - Agent
前端·人工智能·后端·node.js
counterxing1 天前
Agent 跑起来之后,难的是复用、观测和评测
node.js·agent·ai编程
濮水大叔1 天前
告别 Django Admin!这个 NodeJS 全栈框架让你在 DTO 中直接配置 Table/Form 渲染
前端·typescript·node.js