当前位置:网站首页>Three solutions to solve cross-domain in egg framework
Three solutions to solve cross-domain in egg framework
2022-08-05 08:09:00 【M78_Domestic 007】
Option 1: Use the egg-cors plug-in provided by the egg framework to realize cross-domain resource sharing of cors
1. Download the plugin in the project file directory, open the terminal and enter: npm i egg-cors.
2. Open the configuration file config of the project.
2.1. Open plugin.js to open the plugin, and copy this code in the object.
cors:{enable: true,package: 'egg-cors',}
2.2. Open the config.default.js file to configure the plug-in, and copy this code in the arrow function:
config.cors = {origin: '*',allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'}
At this time, cross-domain access can be achieved. The value of the origin attribute here "*" means that all pages can be accessed. We can replace the * with the domain name we specify, but it can only specify one. If you want toSpecify multiple functions that change the origin property value to the following:
config.cors = {// origin: ['http://localhost'],origin:function(ctx) { //Set to allow requests from the specified domain nameconsole.log(ctx);const whiteList = ['http://www.baidu.com','http://www.hqyj.com'];let url = ctx.request.header.origin;if(whiteList.includes(url)){return url;}return 'http://localhost' //By default, local requests are allowed to cross domains},allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'};
Option 2: Implementing jsonp
There are two ways:
1. Configure the following code in the config.default.js file:
config.jsonp = {callback: 'cb', // recognize the `cb` parameter in querylimit: 100, // Specify the maximum character of the function name, here is set to a maximum of 100 characters};
Then do the following in our routing file:
module.exports = app => {const jsonp = app.jsonp();app.router.get("Defined interface address", jsonp, app.controller."Specific location");};
2. Configure directly in the routing file
module.exports = app => {const jsonp = app.jsonp({callback: 'cb',limit: 100,});app.router.get("Defined interface address", jsonp, app.controller."Specific location");};
Option 3: Proxy
Agent is the technology that runs the most in actual development. The request module is no longer used in the egg framework, but the curl() method that comes with the browser is used to directly use the curl method to request resources across domains in our own backend.
The code is as follows:
//In the logic control layer home.jsasync cors(){let data1=await this.ctx.curl("http://www.baidu.com",{method:"GET",data:{pwd:123}})this.ctx.body=data1}
"http://www.baidu.com" is the requested URL, {method:"GET",data:{pwd:123}} can be omitted, method is the request method, and data is the passed parameter.
The data1 obtained at this time is buffer binary data, and we also need toString to convert it into a string.
Additional point:
The processing order of the backend during network requests: Static files> route matching (matching in order), that is, the address of the data interface we set cannot be the same as the address of the statically hosted page, because the execution order is to execute the statically hosted page first, executeAfter that, it will no longer be executed, and we will never be able to request our data interface;
"/*" asterisk routing means that all URLs can be matched.
边栏推荐
猜你喜欢
Vulnhub target drone: HA_ NARAK
Use of thread pool (combined with Future/Callable)
php向mysql写入数据失败
uniapp time component encapsulates year-month-day-hour-minute-second
图扑软件与华为云共同构建新型智慧工厂
Ethernet Principle
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
EA谈单机游戏:仍是产品组合中极其重要的部分
利用Jenkins的持续集成
Ethernet Principle
随机推荐
Embedded Systems: Basic Timers
SVG Star Wars Style Toggle Toggle Button
请问my sql如何把两个表的内容集合在一起啊?
生命的颜色占卜
数据库——概述
【结构体内功修炼】结构体内存对齐(一)
【结构体内功修炼】结构体实现位段(二)
【深度学习实践(一)】安装TensorFlow
Fiddler工具讲解
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
版本号命名规则
RedisTemplate: 报错template not initialized; call afterPropertiesSet() before using it
链表专项之环形链表
ps怎么拼图,自学ps软件photoshop2022,PS制作拼图效果
创业者如何吸引风险投资商
剑指Offer面试题解总结1-10
C语言制作-QQ聊天室
七夕看什么电影好?爬取电影评分并存入csv文件
SQL SERVER关于主从表触发器设计
Illegal key size 报错问题