当前位置:网站首页>nodejs微服务中跨域,请求,接口,参数拦截等功能
nodejs微服务中跨域,请求,接口,参数拦截等功能
2022-08-11 07:59:00 【进击的小民工】
首先说跨域:
前端传输自定义Header
this.ajax({
method: 'GET',headers: {
myToken: global.myToken},url: url,data: data})).then(response=>{
if(typeof response === 'string'){
response = JSON.parse(response);
}
return response
})
注意:myToken为自定义名称,需要在后端指定通过,不然node会拒绝接受,如果不带headers,其实也不会跨域,但是带了请求头后如果后端不做以下操作,就会跨域
后端NodeJS获取自定义Header
通过req.headers.myToken即可获得自定义的头部数据
后端跨域处理
在跨域的情况下,header是限制传输的,因此,首先,需要在后端允许自定义Header
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,myToken");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.all(‘*’,function… 这点代指全部接口
**next()**调用后是放行,否则不会通过任何接口
以此类推,再来说参数拦截
可以从 req.query 中取,比如:
app.all("*", function (req, res, next) {
getSign(req.query.xxx == xxx) ? next(): send('500?')
})
参数处理和请求头处理并不冲突,若没有请求头的话可以单纯处理参数,不用加其他种种~
边栏推荐
猜你喜欢
1.2 - error sources
Keep track of your monthly income and expenses through bookkeeping
零基础SQL教程: 基础查询 05
1076 Wifi Password (15 points)
Test cases are hard?Just have a hand
8、Mip-NeRF
【云原生】云原生在网络安全领域的应用
机器学习(一)数据的预处理
查找最新人员工资和上上次人员工资的变动情况
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
随机推荐
优炫数据库支持多列分区吗?
Do you know the basic process and use case design method of interface testing?
囍楽cloud task source code
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
1101 How many times B is A (15 points)
关于架构的认知
机器学习(三)多项式回归
美术2.4 UV原理基础
Kaldi语音识别工具编译问题记录(踩坑记录)
Distributed Lock-Redission - Cache Consistency Solution
1106 2019 Sequence (15 points)
如何仅更改 QGroupBox 标题的字体?
我的创作纪念日丨感恩这365天来有你相伴,不忘初心,各自精彩
欢迎加入sumarua网络安全交流社区
Write a resume like this, easy to get the interviewer
klayout--导出版图为gds文件
C语言操作符详解
动态代理学习
抽象类和接口
数据库无法启动,报无法分配内存,怎么处理