express-图片防盗链接
思路:当使用本网址的图片的时候,进行防盗链接
图片防盗链姐中间件
imgProtectMid.js
当是本网址使用的图片的时候正常使用,当是其他网址盗用图片的时候,无论他用的那张图,统一返回一个固定的图片.
// const url = require('url');
const path = require('path');
module.exports = (req, res, next) => {
const host = req.headers.host;
const referer = req.headers.referer;
// 只处理图片
const extname = path.extname(req.path);
if (!['.jpg', '.jpeg', '.png', '.gif'].includes(extname)) {
next();
return;
}
const url = new URL(referer).host;
if (url && host !== url) {
req.url = '/image/logo.jpg';
}
next();
}
本网址使用的图片正常展示
其他网址使用的效果