使用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
}
相关推荐
爱吃无爪鱼15 分钟前
03-Bun vs Node.js:JavaScript 运行时的新旧之争
javascript·vue.js·react.js·npm·node.js
爱吃无爪鱼42 分钟前
01-前端开发快速入门路线图
javascript·css·vue.js·typescript·前端框架·npm·node.js
SVIP111591 小时前
webpack入门 精细版
前端·webpack·node.js
十点摆码2 小时前
使用 Jenkins + Gitee + Node 自动化部署 Vue
gitee·node.js·自动化·vue·jenkins
天蓝色的鱼鱼2 小时前
大文件上传实战:基于Express、分片、Web Worker与压缩的完整方案
前端·node.js
思密吗喽3 小时前
如何完全清除Node.js环境重装 Node.js彻底卸载指南
java·开发语言·node.js·毕业设计·课程设计
思密吗喽3 小时前
npm install 报错,解决 node-sass@4.14.1 安装失败问题
rust·npm·node.js·毕业设计·sass·课程设计
ByteCraze3 小时前
CDN 引入 与 npm 引入的区别
前端·npm·node.js
crary,记忆3 小时前
PNPM 和 NPM
前端·学习·npm·node.js
程序员爱钓鱼4 小时前
Node.js 编程实战:安装 Node.js 与 npm / yarn
后端·node.js·trae