它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制。
但在实现上,跟AJAX完全一致,需要的是浏览器和服务器的配合。
浏览器一旦发现AJAX请求跨源,就会自动添加一些附加的头信息,具体来说,就是在头信息之中,增加一个Origin字段。
而服务器端需要处理的字段也是与 CORS 相关的字段,都以 Access-Control- 开头。
服务器收到请求以后,检查了 Origin、Access-Control-Request-Method和Access-Control-Request-Headers 字段以后,确认允许跨源请求,就可以做出回应。
koa-cors.js
* 关键点:
* 1、如果需要支持 cookies,
* Access-Control-Allow-Origin 不能设置为 *,
* 并且 Access-Control-Allow-Credentials 需要设置为 true
* (注意前端请求需要设置 withCredentials = true)
* 2、当 method = OPTIONS 时, 属于预检(复杂请求), 当为预检时, 可以直接返回空响应体, 对应的 http 状态码为 204
* 3、通过 Access-Control-Max-Age 可以设置预检结果的缓存, 单位(秒)
* 4、通过 Access-Control-Allow-Headers 设置需要支持的跨域请求头
* 5、通过 Access-Control-Allow-Methods 设置需要支持的跨域请求方法
module.exports = async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'OPTIONS, GET, PUT, POST, DELETE');
ctx.set('Access-Control-Allow-Headers', 'x-requested-with, accept, origin, content-type');
ctx.set('Content-Type', 'application/json;charset=utf-8');
ctx.set('Access-Control-Allow-Credentials', true);
ctx.set('Access-Control-Max-Age', 300);
ctx.set('Access-Control-Expose-Headers', 'myData');
CORS请求时,XMLHttpRequest对象的getResponseHeader()方法只能拿到6个基本字段:
Cache-Control、
Content-Language、
Content-Type、
Expires、
Last-Modified、
Pragma。
if (ctx.method == 'OPTIONS') {
ctx.body = '';
ctx.status = 204;
} else {
await next();
const Koa = require('koa');
const router = require('koa-router')();
const cors = require('./libs/koa-cors');
const app = new Koa();
app.use(cors);
router.post('/', async function (ctx) {
ctx.body = '跨域成功啦啦啦啦'
});
app.listen(3000);
允许所有域名跨域访问
const cors = require('koa2-cors');
app.use(cors());
并不希望所有人可以去跨域访问接口
指定单个域名跨域
app.use(
cors({
origin: function(ctx) {
return 'http://localhost:8080';
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization']
设置多个域名可跨域
app.use(
cors({
origin: function(ctx) {
const whiteList = ['http://weipxiu.com','http://localhost:8081'];
let url = ctx.header.referer.substr(0,ctx.header.referer.length - 1);
if(whiteList.includes(url)){
return url
return 'http://localhost::3000'
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization']
参考文章:
Koa2使用CORS完成跨域及解决OPTIONS请求
koa2-cors设置允许指定单个域名、多个域名、所有域名跨域
前后端分离项目跨域问题,浏览器控制台报错:No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求方法为OPTIONS,状态值为302或403。
本文解决常见的CORS跨域问题,以及,集成CAS 5.3单点登录内嵌页面时,发送复杂请求产生的跨域问题。
koa2 设置允许跨域需要用到 koa2-cors 插件
npm 地址 https://www.npmjs.com/package/koa2-cors
server.js 中这样写
const Koa = require('koa');
const Router = require('koa-router');
const cors = require('koa2-cors');
let ...
方法1:设置CORS允许跨域
const Koa = require('koa');
const bodyParser = require('koa-bodyparser'); //post数据处理
const router = require('koa-router')(); //路由模块
const app = new Koa();
app.use(async (ctx, next) =&g...
您可以使用wkhtmltopdf模块将HTML文件或字符串转换为PDF。使用URL或内联HTML字符串调用whtmltopdf函数,它将返回一个可以读取或管道传输到您喜欢的任何位置(例如文件或HTTP响应)的流。这是Ubuntu 13.10桌面(64位)的分步指南。1.安装在系统上wkhtmltopdf命令行工具:$ sudo apt-get install wkhtmltopdf2.创建项目目录...
开发的需求中难免会遇到下载文件的要求,下载自定义HTML文件的需求尤为显著。而不是所有的用户都知道HTML,但是知道和使用pdf的肯定多余HTML的,将HTML文件转pdf提供下载优化用户体验自然也就成为各位内卷的又一目标啦!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kRxh9Tvd-1633685073551)(/article/img/AI21bk10pz08.jpg “奋斗”)]
加油奥利给冲起来(别想歪了)
文章目录思路方法一安装举例方法二安装使用api文档总
参考文章 https://blog.risingstack.com/pdf-from-html-node-js-puppeteer/
重点代码片段
const puppeteer = require('puppeteer')
async function printPDF() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
app.use(async (ctx, next)=> {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-...
我希望使用express直接将pdf版本的网页呈现给浏览器.像express.render()这样的东西只将页面呈现为pdf我找到了一个将HTML或URL转换为pdf的模块我需要知道的是如何直接在HTTP路由处理程序中使用来自该库的响应来响应PDF的请求,我宁愿不存储PDF,我只想动态渲染它,然后返回它作为缓冲区或浏览器的流这是该模块提供的基本API:var pdf = require('html...