使用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
}
相关推荐
weixin_571667419 小时前
[解决] Node.js 安装后 命令找不到
node.js
孜孜不倦不忘初心11 小时前
mac安装nvm及问题记录
前端·node.js
快乐的哈士奇14 小时前
Gmail-邮件自动处理系统
node.js·自动化·excel
星空16 小时前
Node.js (Express) + Vue2 Axios 前后端交互 CRUD
vue.js·node.js·express
云浪17 小时前
别再让用户干等了:用 Express + SSE 实现《红楼梦》AI 问答实时输出
javascript·后端·node.js
怕浪猫17 小时前
Electron 开发实战(十四):实战项目|从零搭建轻量化桌面代码编辑器
前端·electron·node.js
zhuxiaojt1 天前
npx 为何如此之慢?浅谈 npx 速度慢的原因及工具推荐
node.js
码农刚子1 天前
从零开始:在 Windows 服务器上部署 Node.js 项目(小白实战教程)
后端·node.js
火山上的企鹅1 天前
Codex实战:APP远程升级服务搭建(一)NodeJS_Express
express
MageGojo2 天前
用 Node.js 把聚合 API 平台封装成零依赖命令行工具:registry 驱动的工程实践
node.js·restful·api接口·命令行工具·cli