发布于 2019-10-26 22:59:18
如果我理解正确的话,您有您的express服务器,并且您希望在响应中包含外来图像,同时隐藏源url。
在最简单的形式中,每当有人请求您的页面时,您将获取所需的图像,将其编码为base64,并将此base64作为img的src包含在内。
const express = require('express')
const fetch = require('node-fetch')
const app = express()
const port = 3000
app.get('/', (req, res) => {
fetch('https://www.gravatar.com/avatar/fdb4d2674d818861be4a4139469ebe59?s=48&d=identicon&r=PG&f=1')
.then(res => res.buffer())
.then(buffer => {
res.send(`
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>hello</p>
<img src="data:image\png;base64, ${buffer.toString('base64')}" alt="image">
</body>
</html>
`)
})
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))理想情况下,您应该为这些映像创建一个单独的端点,并将其缓存(在内存或硬盘上),以避免每次需要时都重新下载它们
https://stackoverflow.com/questions/58571150
复制相似问题