通过nodeJS将其他服务器的图片文件发送给前端

394 阅读1分钟

基于企业微信开发H5项目,业务需要通过img 标签展示微盘里上传的图片,通过企业微信开放的API接口获取下载微盘文件的URL,这个URL需要携带对应的Cookie才能正确返回我需要的图片。而****img标签不能携带Cookie,我能想到的是通过nodeJS携带Cookie请求图片后返回给前端,下面是一个简单的demo

路由模块

router.get('/getFile', indexHandler.getFile)

函数处理模块

exports.getFile = (req, res) => {
    request({
        method: "GET",
        headers: {
	    'Cookie':'FTN5K=ba1e8eba'
        },
        encoding: null,
	url:'https://sh-btfs-v2-down.wework.ftn.qq.com/ftn_handler/680b5704cbc7c45c71640dbd109edd5618603567fb822d1e9dee92d5d54e125d2744c320e321f5ad7c745fec886c485e2aaf5e16cb37508d49f4421e52b5ba02?fname=%E5%A4%8F%E5%AD%A6%E7%94%9F-%E4%BA%8C%E5%B9%B4%E7%BA%A71%E7%8F%AD-4667C6F5-D42F-4877-81E4-BB5D271E22AD.png',
}, (error, response, body) => {
        res.set('Content-Type', 'image/png;') 
	res.send(body)
    })
	
}

测试