使用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
}
相关推荐
烂蜻蜓13 小时前
Node.js入门教程(十一):异步编程
node.js
Sca_杰15 小时前
微信客服API对接速通
javascript·node.js
FungLeo18 小时前
Node 后端实战 · D1 那些坑:100 参数上限逼出的批量写入重构
node.js·serverless·d1 数据库·sqli
谦卑a19 小时前
nodejs安装保姆级教程
前端·javascript·node.js·个人开发
烂蜻蜓20 小时前
Node.js入门教程(十八):Stream(流)
node.js
东方小月2 天前
从零开发一个 Coding Agent(九):实现 Agent 的工具调用闭环
人工智能·前端框架·node.js
Yolanda_20222 天前
nvm安装Node卡顿解决方案:两行配置切换国内镜像源
node.js
王八八。2 天前
nvm 安装与配置完全指南:从零到多版本 Node.js 自由切换
node.js
紫禁玄科2 天前
Shai-Hulud:npm生态的自我复制蠕虫风暴
前端·npm·node.js
烂蜻蜓2 天前
Node.js入门教程(十四):async/await
node.js