node.js - 如何将 cookie 传递给响应 expressjs?
问题描述
我需要通过
授权Cookie
为了表达响应,我使用技术 JSONP 绕过谷歌的 CORS,但我不知道如何将 cookie 传递给响应。所以客户端浏览器可以获取该cookie。最终结果是带有 cookie 的直接 google drive 链接。目前只传递直接链接,但 cookie 为空。我不知道。
客户端html
<video id="video-player" controls></video>
<script>
const videoPlayer = document.getElementById('video-player');
(async () => {
const response = await fetch('http://127.0.0.1:8080/video');
videoPlayer.src = await response.text();
})();
</script>
服务器
const express = require('express');
const htmlParser = require('node-html-parser');
const { parse } = htmlParser;
const cors = require('cors');
const fetch = require('node-fetch');
const app = express();
const port = 8080;
app.get('/video', cors(), async (req, ress) => {
const response = await fetch('https://drive.google.com/uc?id=17OunL0rN-
7jrsNGjznQTqnB_z7xfsgKK&export=download', { redirect: 'manual' });
const dom = parse(await response.text());
const url = 'https://drive.google.com' + dom.querySelector('#uc-download-link')._attrs.href;
const resCookie = await response.headers.get('set-cookie').split(';');
const downloadCookie = resCookie[0];
const nidCookie = resCookie[6].split(',')[1];
const reqCookie = downloadCookie + ';' + nidCookie;
let options = {
headers: {
cookie: reqCookie
},
redirect: 'manual'
};
const res = await fetch(url, options);
const re = await fetch(await res.headers.get('location'), {redirect: 'manual'});
const authCookie = await re.headers.get('set-cookie').split(';')[0];
const r = await fetch(await re.headers.get('location'), { headers: { cookie: nidCookie }, redirect: 'manual' });
const data = await fetch(await r.headers.get('location'), { headers: { cookie: authCookie}, redirect: 'manual'});
ress.send(await r.headers.get('location'));
});
app.listen(port , () => {
console.log(`Server is listening on ${port}`);
});
解决方案
您可以使用 res.cookie(name, value) 通过 express 设置 cookie。你可以在这里阅读更多关于它的信息。
推荐阅读
- javascript - Vuejs2窗口获得滚动高度
- php - PHP Xdebug 不在 php 中
- python - 使用 WTForms Flask 解析日期 - Python
- javascript - Ember 为什么我们必须对某些 bower 依赖项使用 import
- javascript - 将 JSON 信息存储在 HTML 文件中或以其他方式存储
- python - Python - 用于 GIS 和生成多边形的几何基元
- webpack - Webpack 动态导入中的字符串文字
- android - Parcelable 示例错误。
- swift - SKTextureAtlas 留在记忆中
- c - Issue with scanf and pointers